Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need help with virtualhost settings in apache2. I do not want users to access my website directly using a server ip like and want to ensure that it opens only when the particular domain ( is typed in. The direct ip access should result in a redirect the

share|improve this question

migrated from Aug 17 '10 at 5:20

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

Using mod_rewrite this should work

RewriteCond %{HTTP_HOST}   !^example\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/?(.*)$1 [L,R,NE]

But this should be moved to

share|improve this answer

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

Your Answer


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.