Redirect some pages from http to https using apache mod_rewrite

  • Avinash
  • 0
  • Aug 16, 2010
  • Web Server

In previous post ( here ) I have described that how can we redirect all pages of any website from http to https protocol.

In 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.

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]
   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 Check here.

Related Posts

Written by Avinash

Avinash Zala currently working at a leading web development company as a Sr. PHP Web Developer. He is the founder of the Xpert Developer. He has completed B.E. in Information Technology. Born and brought up in Ahmedabad. He loves to code and coding is all about passion for him. Follow him on twitter at Avinash Zala & Xpert Developer.

View all posts by: