I need help with virtualhost settings in apache2. I do not want users to access my website directly using a server ip like http://55.66.77.88 and want to ensure that it opens only when the particular domain (example.com) is typed in. The direct ip access should result in a redirect the example.com.
2 Answers
Using mod_rewrite this should work
RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://example.com/$1 [L,R,NE]
But this should be moved to serverfault.com
Just create a default VirtualHost which will be defined before any other VirtualHost. If the HTTP/1.1 Host
header contains a domain which no VirtualHost exists for, no domain at all or if HTTP/1.0 is used, the default VirtualHost will be served to the client.
You can easily put a RedirectPermanent
directive inside that default VirtualHost.
For details see In-Depth Discussion of Virtual Host Matching in the Apache httpd documentation.
If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected (as already mentioned before).