Search code examples
apachemod-rewritehttp-status-code-301

301 Redirect not working on new server


We are running Apache servers on Linux.

In the <VirtualHost> of the Apache Config file (in /etc/apach2/sites/site.conf), we have the following rewrite rule:

The purpose of this rule is so that if anyone accesses the site (which can be accessed with multiple domains) attempts to access without www. in front, it will 301 redirect to www.. For example, if a user goes to the site http://example.com, the rule will 301 redirect to http://www.example.com.

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([a-z.]+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

We have this same rule setup on other servers and working fine, but for some reason we just can't get it working on this new server. Restarting Apache is fine etc.. but when we access the site with example.com, it is not redirecting to www.example.com.

Any suggestions or previous experiences that may give some clues to cause?

=========

Here is an expanded version of the Config for context.

<Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/site/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all

                RewriteEngine on

                # force www
                RewriteCond %{HTTP_HOST} !^www\. [NC]
                RewriteCond %{HTTP_HOST} ^([a-z.]+)$ [NC]
                RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

                # Bypass images, css, javascript and docs, add your own extensions if needed.
                RewriteCond %{REQUEST_URI} \.(bmp|gif|jpe?g|png|css|js|txt|pdf|doc|xls|ico)$
                RewriteRule ^(.*)$ - [NC,L]

                # The ColdBox index.cfm/{path_info} rules.
                RewriteRule ^$ index.cfm [QSA,NS]
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteRule ^(.*)$ index.cfm/%{REQUEST_URI} [QSA,L,NS]

        </Directory>

Solution

  • Comment: I've added the RewriteRule as you suggested.. and that seems to be rewriting fine. 119.9.30.242/foo rewrites to http://119.9.30.242/bar?host=119.9.30.242

    But in the question you stated:

    ...but when we access the site with example.com, it is not redirecting to www.example.com

    You appear to be accessing the site by an IP address, not a domain name?!

    If you are accessing the site by the IP address (not a domain name) then this is obviously not going to redirect, since the preceding RewriteCond directive validates the hostname:

    RewriteCond %{HTTP_HOST} ^([a-z.]+)$ [NC]
    

    This only allows hostnames that consist of letters and dots, no digits or hyphens (as I mentioned in my previous comment), so this will exclude IP addresses from being redirected. (And correctly so, http://www.119.9.30.242/ would be invalid.)

    UPDATE: To allow/redirect domains with digits (and hyphens) then you would need to change the above directive/regex to:

    RewriteCond %{HTTP_HOST} ^([a-z0-9.-]+)$ [NC]