htaccess重定向到HTTPS所有页面,除了一个页面


htaccess redirect to https all pages except one

我正在使用Yii框架。我想将网站上的所有页面从HTTP重定向到HTTPS,除了这个 domain.com/clip/create

以下代码将所有页面重定向到 HTTPS:

RewriteEngine on
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

我修改了该代码以添加一个页面的异常,但它无法正常工作。它适用于网站上的所有页面,但在地址 domain.com/clip/create 它重定向到 https://domain.com/index.php我的代码如下:

RewriteEngine on
RewriteCond %{HTTPS} !^on$
RewriteCond %{REQUEST_URI} !^/clip/create
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
RewriteCond %{HTTPS} ^on$
RewriteCond %{REQUEST_URI} ^/clip/create
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R,L]    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

任何帮助都非常感谢。

谢谢

你想删除第二个重定向的"https"部分,(你可能也不需要!-f!-d检查):

RewriteCond %{HTTPS} ^on$
RewriteCond %{REQUEST_URI} ^/clip/create
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R,L]    
# no "s" here ---------^

这可能是因为你的 Yii 控制器。在将请求路由到index.php控制器,需要防止再次重定向。将其添加到最顶部(在重定向之前):

RewriteRule index'.php - [L]

我对 htaccess 很糟糕

RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} !^/clip/create [OR]
RewriteRule ^/directory(.*)$ https://%{HTTP_HOST}/directory$1 [L,R]

从这里获得规则

清除浏览器缓存!当我在HTTP和HTTPS之间切换连接类型时,我遇到了许多这些问题,然后经过大量搜索和尝试,我发现Firefox和chrome缓存了301和302错误代码。

这将适用于您:

RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_URI} !/clip/create
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php