Redirect Http to Https Using Htaccess
Redirect Http to Https Using Htaccess | htaccess redirect to https://www
Published
Always make sure your website follows Google recommendation, and as per recommendation secure your website with an SSL Certificate. It fulfils two major benefits, first it serves as a primary SEO factor for search engines and second creates trust for website users.
After configuration of SSL from cpanel or hosting configuration, you need to redirect all web traffic from ‘http’ to ‘https’. You can easily achieve that using a .htaccess configuration file in the Apache web server domain folder.
- You can create a new ‘.htaccess’ file if it is not there in the website root domain folder.
- 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 in the beginning of the existing code.
1-Code to redirect all links from ‘http’ to “https” and non www to www (Best Practice)
Note: replace your-website-url.com with your own website name on both locations
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^your-website-url.com$ [NC]
RewriteRule ^(.*)$ https://www.your-website-url.com/$1 [L,R=301]
</IfModule>
RewriteCond %{HTTP_HOST} The HTTP_HOST server variable contains the value of the Host HTTP request header (ie. the hostname), this is irrespective of the protocol used (HTTP or HTTPS).
So, based on RewriteCond says true means condition match then will be redirect rule will according to RewriteRule.
2-Code to redirect all links from ‘http’ to “https”
Note: replace your-website-url.com with your own website name on both locations
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^your-website-url.com [NC]
RewriteRule ^(.*)$ https://www.your-website-url.com/$1 [L,R=301]
</IfModule>
RewriteCond %{HTTP_HOST} The HTTP_HOST server variable contains the value of the Host HTTP request header (ie. the hostname), this is irrespective of the protocol used (HTTP or HTTPS). If Yes then will be redirect rule will redirect to port 443 https.
3- Code to redirect all links from ‘http’ to “https”
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
</IfModule>
RewriteCond %{SERVER_PORT} 80 check the request for web page or resource coming through port 80. If Yes then will be redirect rule will redirect to https.
[R=301,L] – Here, L means “last rule” to stop further rules processing if this rule matched.
4- Code to redirect all links from ‘http’ to “https”
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>
5- Code to redirect all links from ‘http’ to “https”
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>