簡體   English   中英

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM