Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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 want that when people access my web server with the ip address, like, be redirected to the domain name, like I'm using Apache Web Server and I've tried:

    Redirect /

which generated an infinite recursion because it has higher priority than

<VirtualHost *:80>

which is the virtual host I ultimately want to serve.

I've also tried

    Redirect /

thinking that maybe it'll only match but not But it also generate an infinite loop. Any ideas how to achieve that?

The reason why the IP virtual host is not the main and only virtual host is because I want other IPs on the server to also be able to server

share|improve this question
What about your NameVirtualHost setting ? – radius Jul 22 '09 at 5:23

HD answered gave a good rewrite rule, but you should put a condition before that. Make sure that there is only one virtual host running on and have your definition look like the following:


    RewriteCond %{SERVER_NAME}
    RewriteRule /(.*)$1 [R=301,L]


This way, you only need one virtual host for that IP address instead of what you were trying to do above. NOTE: If you're going to have more than one virtual host on this server, you need to put the rest on different IP(s)

share|improve this answer
Shouldn't the rewrite condition try to match the virtual host ip, like instead of Why would other virtual hosts need to run in different IPs? Won't they match by name instead of IP? – J. Pablo Fernández Aug 5 '10 at 8:30
In my case it was also neccessary to add RewriteEngine On before the RewriteCond line. – lanoxx May 8 '12 at 20:36

I try the same configuration and it works fine. Are you sure there is no other definition that conflicts with your virtual hosts? What is the error in the apache logs?

This rule also works:

RewriteRule ^(.*)$$1 [R=301,NC]
share|improve this answer

in .htaccess


RewriteCond %{HTTP_HOST} ^ RewriteRule (.*)$1 [R=301,L]

share|improve this answer
What does this add to the answer from seven years ago? – Sven Mar 21 at 12:14
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review – Dave M Mar 21 at 14:15

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.