Htaccess Redirect Http to Https

HTTPS allows secure communication between your browser and the server for your website. If your hosting server provides SSL certificate, you can redirect all http (non-secure) URLs to secure https.

Google also recommends using Secure URLs and it also one of the important SEO factor. Having secure URL (lock symbol in URL bar) creates trust for users which is very important for websites having any kind of payment feature or confidential information.

Once you implemented SSL, you may want to redirect all web traffic from ‘http’ to ‘https’ using a .htaccess configuration file in the Apache webserver.

Code to redirect all links from ‘http’ to ‘https’:

You can create a new '.htaccess' file if it is not there in the website root. If it is already there, you need to put the following code in '.htaccess' file. If it does not work with the existing code, you can try placing it at the beginning of the existing code.

<IfModule mod_rewrite.c>
RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$$1 [R=301,L]

<IfModule> is Apache directive that checks if mod_rewrite module is loaded.
RewriteEngine On Enables runtime rewriting engine.
RewriteCond %{SERVER_PORT} 80 restrict the rewrites to requests on port 80.
RewriteRule creates a rule to rewrite one URL to another.
[R=301,L] – Here, R=301 force 301 (permanent) redirects.
[R=301,L] – Here, L means “last rule” to stop further rules processing if this rule matched.

SEO Recommends 301:
If you are permanently redirecting URLs, you should use 301 redirects to maintain SEO, else 302 can be used for temporary URL redirects.

Note: Note: If redirection code does not work, try placing code in the beginning of .htaccess file.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.