How to force SSL (redirect) using .htaccess

When your website has SSL enabled, you may want to force all traffic to be served over the SSL encrypted connection only. This can easily be achieved by adding the code below in your .htaccess file. If you have any redirect codes in your .htaccess file, we recommend adding this before that and verifying that no other redirect code is redirecting back to http:// non-SSL URLs.

The code to be added in your .htaccess file is as follows. Replace domainname.com with your own domain.

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domainname.com/$1 [R,L]

Did you find this article useful?