Friday, April 3, 2015

How to redirect https to http in htaccess?

Today, htaccess play very important role for SEO, It is used for redirectionrewriting and apache config variable etc.
How to edit your htacess file.
Open your .htaccess file which is mostly in root folder OR in public/public_html folder.
Use following code as per your requirement.(Please have backup your .htaccess file before editing).

https://example.com to http://example.com redirect 
?
1
2
3
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

https://example.com to http://www.example.com redirect
?
1
2
3
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


http://example.com to https://example.com redirect
?
1
2
3
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]


http://example.com to https://www.example.com redirect 
?
1
2
3
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Redirect from http to https using htaccess

Some case you want that only some of the pages to redirect to https but not all pages.
In that case the code in above post will not work hence it will redirect all pages to https.
To redirect only some of the pages you need to use below code in your htaccess file.


RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} order.php [OR]
RewriteCond %{REQUEST_URI} payment.php [OR]
RewriteCond %{REQUEST_URI} success.php
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
From above code in your website only order.php, payment.php and success.php will be redirect to https protocol.
Here I have used RewriteCond with OR condition. If we use OR condition than Rule will be apply if any of the condition gets true. For more detail about OR condition

No comments:

Post a Comment