Cannot get my site to force www. keeps showing a 404 (https urls) or 403 (http urls) server error.

<IfModule mod_rewrite.c> 

    # Force HTTPS
        RewriteEngine On
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

    # Add www
        RewriteCond %{HTTP_HOST} !^www\.
        RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [R=301,L] 

    # Removes index.php from ExpressionEngine URLs
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

  # Directs all EE web requests through the site index file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]


Try to combine your redirects, so you minimise the amount of jumps as it's bad SEO to have too many. Your first chunk goes to https, but your second jump (forcing www) doesn't - which may explain your different server errors.

Redirect your incorrect domain first (to the correct https version), then the https, therefore if someone isn't accessing the URL with the www and without the https, they'll be put right by the first redirect.

# redirect to www version
# RewriteCond %{HTTP_HOST} ^www.yourdomain.co.uk [OR] - Example for multiple domains
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ https://yourdomain.com/$1 [L,R=301]

After the above, add the https detection...

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,L]

If you want to only secure certain URLs, add the following to the beginning of the code above:

RewriteCond %{REQUEST_URI} (checkout|account)

Depending on the server, the %{HTTPS} detection may not work and you have to check for port instead:

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,L]
  • Thanks, I tried your code but still not redirecting from non www to www. Wonder if it may be the DNS? – Thomas K Dec 6 '15 at 16:10
  • Your DNS should have 2 "A" records: mydomain.com and *.mydomain.com (to catch the subdomains) - both pointing at your IP. Your http.conf (or http_vhosts.conf?) on the server may need to specify the the www as an alias. – Peter Lewis Dec 6 '15 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.