我的网站启用了 HTTPS,并且所有页面均仅使用 HTTPS 提供服务。客户现在有这样的需求,他希望将 about-us
、termsofus
等静态页面显示为 HTTP 页面,而不是 HTTPS。这意味着即使用户尝试以 HTTPS 方式打开 about-us
页面,它也应该重定向到 about-us
的 HTTP 版本。
我的.htaccess
代码如下:
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^\/about-us
RewriteCond %{REQUEST_URI} !^\/termsofus
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} \/about-us [OR]
RewriteCond %{REQUEST_URI} \/termsofus
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule>
问题:每当我打开 HTTP/HTTPS 版本的 about-us
页面时,它都会将我重定向到 index.php
。
例如:https://example.com/about-us
到 https://example.com/index.php
该网站使用PHP YII框架。
最佳答案
使用THE_REQUEST
变量而不是REQUEST_URI
。 THE_REQUEST
变量表示 Apache 从浏览器接收到的原始请求,并且它在执行某些重写规则后不会被覆盖。
Options -Indexes
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+(about-us|termsofus) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/+(about-us|termsofus) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
在测试此更改之前,请确保清除浏览器缓存。
关于php - 静态页面的 htaccess 条件 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44282823/