Redirect from non-https (http) to https using htaccess Print

  • htaccess
  • 2

Redirecting from HTTP to HTTPS is very easy and a lot of control panels such as cPanel have redirect managers to help you with this, but if you want to add the code yourself the best option is htaccess.

If you don't have a .htaccess file already in your webroot, just create a new file and name it '.htaccess' (with no other extension and nothing before the '.'):

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$$1 [R,L]

With the above code, we are checking if we are running on port 80 which is not a secure port by default, 443 is the secure port, so seeing the condition of running on port 80 is met we direct with our 'RewriteRule' to our domain with https. You will notice '$1' at the end of the URL, this ensures the URL params after the trailing slash is kept on redirect. 

Was this answer helpful?

« Back