Today, in this article, we will explain to you how to redirect HTTP to HTTPS using htaccess file. it has forces any HTTP request to redirect to HTTPS using the below example.
If you have a purchase SSL certificate and http://www.example.com domain. and you want to redirect to https://www.example.com. it is possible to the .htaccess. first, create a .htaccess file in your root project directory and paste the following code.
Note: the Apache mod_rewrite module must be enabled. Otherwise, the following snippet will not work. see the following code example.
Redirect All Web Traffic
RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
Redirect Only a Specific Domain
RewriteEngine On RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC] RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
Redirect Only a Specific Folder
RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteCond %{REQUEST_URI} folder RewriteRule ^(.*)$ https://www.yourdomain.com/folder/$1 [R,L]
Force Any HTTP Request to Redirect to HTTPS
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Please follow and like us: