Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force rewrite to HTTPS except for a few pages in Apache?

I need to force redirect all the pages in Apache to HTTPS except for a few pages. How to write rewrite rule in Apache for this condition?

like image 382
Gnanz Avatar asked Apr 28 '11 11:04

Gnanz


2 Answers

RewriteEngine On

RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} !^\/page1\/
RewriteCond %{REQUEST_URI} !^\/page2\/
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]    

RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} \/page1\/ [OR]
RewriteCond %{REQUEST_URI} \/page2\/
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]

The first rule-set will redirect all pages not accessed via HTTPS, and that are not /page1/ or /page2/ to the same URL but https://. The second rule-set will make sure that /page1/ and /page2/ are redirected back to http:// if they are accessed via https://.

like image 177
clmarquart Avatar answered Nov 10 '22 20:11

clmarquart


A more simple solution:

RedirectMatch ^((?!\/(page1|page2)).*)$ https://%{HTTP_HOST}$1

This will redirect everything except page1 and page2 to https of the current host.

like image 9
klodoma Avatar answered Nov 10 '22 19:11

klodoma