How to Redirect to WWW and HTTPS

Posted by Paul on March 4, 2021

I've mentioned before that you should always use WWW, and I've also blogged that it's time to switch to HTTPS. Here's how to set up the redirects in .htaccess (applies to all sites, OpenCart or not) and also make sure OpenCart is configured correctly for it in its 2 config.php files.

So, to force WWW and HTTPS, you'll need this in your .htaccess just after the line "RewriteBase /" (which may include a sub-folder which is fine):

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

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

https://stackoverflow.com/questions/24711083/forcing-ssl-and-www-using-htaccess

Then you'll need to edit both your config.php files:

/config.php
/admin/config.php

And simply make sure ALL links to the website have both WWW and HTTPS.

And that's it!

blog comments powered by Disqus