Gna*_*anz 7 apache mod-rewrite https
我需要强制将Apache中的所有页面重定向到HTTPS,除了几页.如何在Apache中为这种情况编写重写规则?
clm*_*art 22
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]
Run Code Online (Sandbox Code Playgroud)
第一个规则集将重定向所有未通过HTTPS访问的页面,这些页面不是/page1/或者/page2/是相同的URL但是https://.如果通过访问它们,第二个规则集将确保/page1/并/page2/重定向回.http://https://
更简单的解决方案:
RedirectMatch ^((?!\/(page1|page2)).*)$ https://%{HTTP_HOST}$1
Run Code Online (Sandbox Code Playgroud)
这会将除page1和page2之外的所有内容重定向到当前主机的https.