Apache ModRewrite not working with foo.com, but works with www.foo.com

Posted on

Apache ModRewrite not working with foo.com, but works with www.foo.com – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about apache-2.2, mod-rewrite, , , .

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.

Solution :

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.

Leave a Reply

Your email address will not be published.