Redirect from www to non-www not working

992 views Asked by At

I have a site running on Craft CMS, I am not sure if the issue is directly to do with the CMS or the server.

I am using .htaccess to rewrite any www url to a non-www version

<IfModule mod_rewrite.c>

    # (1)
    RewriteEngine On

    # (2)
    Options +FollowSymlinks

    # (3)
    #Options +SymLinksIfOwnerMatch

    # (4)
    #RewriteBase /

    # (5)
    #RewriteOptions <options>

    # (6)
    RewriteCond %{HTTPS} =on
    RewriteRule ^ - [env=proto:https]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ - [env=proto:http]


    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]

</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On
#   RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
</IfModule>

When I type domain.com/about it works fine, however when I type www.domain.com/about it changes the URL to domain.com/index.php?p=about

In the Craft config I have 'omitScriptNameInUrls' => true, enabled, which along with the below should remove the index.php from the URL.

RewriteEngine On

# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]

Here is the the vhost file.

<VirtualHost *:80>
    ServerName domain.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/

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

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I have looked through https://httpd.apache.org/docs/2.4/rewrite/remapping.html#canonicalhost, but not had any luck with any changes.

I am using Ubuntu 16.04.1, Apache 2.4.18 and 7.0.8-0ubuntu0.16.04.3 Hosted on Ubuntu.

Is there anything I can test to see if I can get the redirect for the www to the non-www working correctly?

2

There are 2 answers

1
Capsule On BEST ANSWER

You need to move this block before the other expressions:

<IfModule mod_rewrite.c>
    RewriteEngine On
#   RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
</IfModule>

Although you have the L tag in the previous one, so it should be the last rule applied, it doesn't work (probably because your activate the RewriteEngine again later so it resets the rules, but if the L flag was active in the first rule, it wouldn't apply the www one anyway).

The rule of thumb is: first do your cosmetic changes to the domain, then apply the functional rules.

You also don't need to open 2 IfModule conditions. Combine the 2 in one as this:

<IfModule mod_rewrite.c>

    # (1)
    RewriteEngine On

    # (2)
    Options +FollowSymlinks

    # (3)
    #Options +SymLinksIfOwnerMatch

    # (4)
    #RewriteBase /

    # (5)
    #RewriteOptions <options>

    # (6)
    RewriteCond %{HTTPS} =on
    RewriteRule ^ - [env=proto:https]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ - [env=proto:http]

#   RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]

</IfModule>
0
Ruben On

Force it!

RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ http://domain.com/$1 [R,L]