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

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

+6
source share
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] 

In the first set of rules, all pages that cannot be accessed via HTTPS and which are not /page1/ or /page2/ for the same URL but https:// will be redirected. The second set of rules ensures that /page1/ and /page2/ are redirected back to http:// if they are accessed via https:// .

+18
source

A simpler solution:

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

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

+5
source

Source: https://habr.com/ru/post/886871/


All Articles