Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

migrated from stackoverflow.com Aug 17 '10 at 5:20

This question came from our site for professional and enthusiast programmers.

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

share|improve this answer
add comment

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.