Take the 2-minute tour ×

I can't get the RewriteCond %{HTTP_HOST} to trigger when I just use foo.org. It just seems to ignore it, but www.foo.org and sub.foo.org seem to trigger the rewrite condition. I'm probably missing something simple, and was hoping some more experienced eyes would be able to help.

<VirtualHost *:80>
    ServerName foo.org
    ServerAlias sub.foo.org www.foo.org

    DocumentRoot /var/www/

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.foo.org$
    RewriteRule ^/(.*)$ http://sub.foo.org/$1 [L,R]

I've tried these 3 settings (replacing the above)

    RewriteCond %{HTTP_HOST} ^www.foo.org$
    RewriteRule ^/(.*)$ http://sub.foo.org/$1 [L,R]  # WORKS :)

    RewriteCond %{HTTP_HOST} ^sub.foo.org$
    RewriteRule ^/(.*)$ http://www.foo.org/$1 [L,R] # WORKS :)

    RewriteCond %{HTTP_HOST} ^foo.org$
    RewriteRule ^/(.*)$ http://sub.foo.org/$1 [L,R] # DOESN'T WORK!!!

This is really frustrating for me, because it looks so simple, but I am unable to figure it out. Does anyone have any ideas? All of DNS entries are set (i can access all of these sites directly from the browser, just the redirection is broken).

UPDATE: There was another virtualhost enabled under sites-enabled. After disabling it, everything seems to work. I don't really understand why, but i think that somehow the other virtualhost's settings were being used when foo.com was entered, and thus apache didn't find any RewriteRules.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Try to enable mod_rewrite debug with the following directive.

Start at level 3 and show us the result. Also please add your apache version.

share|improve this answer
I was unaware of mod_rewrite debug. It helped me find out the above problem. Thanks for your help. –  Bill Zimmerman Aug 27 '10 at 13:50
add comment

Your Answer

