Permanent redirection to default host in Apache

A few weeks ago at work, I needed to rename a webhost. To avoid breaking a lot of links to the old hostname, I set up permanent redirection, but ran into an infinite loop. I tried both a simple Redirect statement like this:

Redirect permanent / http://newhost/

And the same thing using mod_rewrite. I have done this many times before at home, but what was special about this case is that the web server was set up as default host. So I just added a virtual host with the old hostname. Eventually I got it to work with mod_rewrite by using a RewriteCond statement to break the loop:

<VirtualHost *:80>
ServerName oldhost

<IfModule rewrite_module>
RewriteEngine on
RewriteCond %{HTTP_HOST} oldhost
RewriteRule ^/(.*)$ http://newhost/$1 [R=permanent,L]
</IfModule>
</VirtualHost>

However, I’m still not sure why this is neccesarry, since the first rewritten URL should end at the default host which doesn’t rewrite anything.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.