Learn how to redirect website traffic from HTTP to HTTPS using a .htaccess file in this quick and easy tutorial.
Insert the following lines at the very beginning of the .htaccess
file in your website’s folder.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
</IfModule>
Be sure to replace example.com
in the above code with your actual domain.
If a .htaccess
file doesn’t exist in your website’s folder, simply create a new one. Keep in mind that the filename begins with a period (.
).
For subdomains such as sub.example.com
, use the following code instead:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^sub.example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.sub.example.com$ [NC]
RewriteRule (.*) https://sub.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.sub.example.com$ [NC]
RewriteRule (.*) https://sub.example.com/$1 [R=301,L]
</IfModule>
Be sure to replace sub.example.com
in the above code with your actual subdomain.
That’s all there is to it! Your website visitors should now be redirected from HTTP to HTTPS.
You may also like:
Love our articles? HostM offers professional and helpful web hosting services with unlimited features and renewal rates that actually match our advertised rates.
From $2.48/mo
We can help you move your
email and websites as desired.