Force SSL only HTTPS:// 301 redirect using .htaccess file

This is how you can force SSL redirection to https:// URL using a .htaccess file with a 301 redirect.

If you do not have a .htaccess file then create a TXT file using notepad or any other text editor. Copy the code and rename it to .htaccess. Upload it to your website's root directory.

If you already have the .htaccess file then just edit it and put these following code.


RewriteEngine on


If you already have the RewriteEngine on in your .htaccess file then skip this and add only the following line.

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


If you want to add www along with https:// then add the following code.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


Here R=301 means this will get a 301 permanent redirect. You can put 302 instead if you want a temporary redirect.

The above code will redirect any http:// URL to https://www">https://www URL. However if anyone type https:// that it will not redirect with www. To correct the error please use the following code.


#For http:// url it will redirect with https://www 
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


If you want to add www along with https:// then add the following code.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#For https:// url it will redirect with https://www
RewriteCond %{HTTPS} on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


If you want to add www along with https:// then add the following code.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


Hopefully, This article will help you.
  • .htaccess, 301 Redirect, https
  • 344 Users Found This Useful
Was this answer helpful?