php - 静态页面的 htaccess 条件 https

标签 php .htaccess yii

我的网站启用了 HTTPS,并且所有页面均仅使用 HTTPS 提供服务。客户现在有这样的需求,他希望about-ustermsofus 等静态页面显示为 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-ushttps://example.com/index.php

该网站使用PHP YII框架。

最佳答案

使用THE_REQUEST变量而不是REQUEST_URITHE_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/

相关文章:

php - 包含一个已在 php 中包含文件的文件 [(!)警告]

php - 如何在 PHP 中将 UNIX 时间戳转换为 PST

php - preg_match_all 返回模式匹配而不是主题匹配

php - .htaccess PHP 5.3 选项打破了 Access-Control-Allow-Origin 选项?

php - htaccess 图片 url 重写规则

php - Yii 应用程序未激活的触发器

javascript - 将页面信息加载到数组中,并将页面信息循环作为 URL

.htaccess - htaccess 从 https 中排除 sitemap.xml 和 robots.txt

activerecord - Yii ActiveRecord 和缓存中的 beforeFind()

php - YII - 单元测试不起作用