How to Write Redirect Rules for HTTP to HTTPS in Apache using .htacess
HTTP vs HTTPS
HTTP (HyperText Transfer Protocol) is the most basic and known protocol (a standard set of rules) used to transfer data from a web server to a browser. It allows communication between different systems.HTTPS is HTTP with encryption i.e it uses encryption (TLS/SSL) to make HTTP requests more secure while transferring the data from a web server to a browser or vice-versa.
So there are benefits of using HTTPS for your website:
1. It makes your website more secure against data theft.
2. It also improves your SEO rank.
So now let's learn how we can direct a website from HTTP to HTTPS.
First, make sure you have generated and integrated SSL certificate for your website and the Rewrite module is enabled on your hosting server. so that your website can work on HTTPS without any trouble.
After applying the SSL certificate our website can be accessed via HTTP or HTTPS. But per above after knowing the benefits of HTTPS we need redirect HTTP to HTTPS.
Re-write Rules
Here is I assume that you are a bit aware of setting basic apache settings via .htaccess file, like - rewrite rules, updating memory size, etc.Here we will only talk about re-write rules for apache. Basically, re-write rules have 2 parts - RewriteCond i.e. condition for action to be in-effect and RewriteRule i.e. the action itself.
The condition can be simple (only one RewriteCond ) or can be complex (having multiple RewriteCond ) combined with logical operator `AND`(default and option to write) and `OR`.
Redirect HTTP to HTTPS for Domain
To redirect from HTTP to HTTPS we simply need to have 2 conditions and 1 Rule as below:RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^yourdomain.com$
RewriteRule ^(.*)$ https://yourdomain.com/$1 [QSA,L,R=301]
First condition checks if HTTPS is off i.e. if the incoming request is over HTTP only.
Second condition checks if the request for your HOST domain.
If both the conditions result in TRUE the rule will be in action and it redirects all the requests over HTTPS.
Redirect HTTP to HTTPS for Domain with Sub-Domains
To redirect from HTTP to HTTPS for a domain with Sub-Domain you have added 1 additional Condition and little update in Rule which we have done in case of simple domain. i.e.
RewriteCond %{HTTP_HOST} ^((.+)\.)?yourdomain.com$
The above condition checks if the incoming request is for any sub-domain of your domain of any character(using regex ``).
Now update your Rewrite Rule to include subdomain part by adding `%1`(which will take regex matching part i.e. subdomain) while redirecting to HTTPS as:
Now update your Rewrite Rule to include subdomain part by adding `%1`(which will take regex matching part i.e. subdomain) while redirecting to HTTPS as:
RewriteRule ^(.*) https://%1.yourdomain.com/$1 [QSA,L,R=301]
So complete Rewrite Rule block will look like:
RewriteCond %{HTTPS} offRewriteCond %{HTTP_HOST} ^yourdomain.com$
RewriteCond %{HTTP_HOST} ^((.+)\.)?yourdomain.com$
RewriteRule ^(.*) https://%1.yourdomain.com/$1 [QSA,L,R=301]
Here one thing to note here, in .htacess we can access any server global variable like - HTTP_HOST, REQUEST_URI, HTTP, etc.
Comments
Post a Comment