簡體   English   中英

靜態頁面的htaccess條件HTTP

[英]Htaccess conditional https for static pages

我的網站已啟用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>

問題:每當我打開about-us頁面的HTTP / HTTPS版本時,它都會繼續將我重定向到index.php 例如: https://example.com/about-us : https://example.com/about-ushttps://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

在測試此更改之前,請確保清除瀏覽器緩存。

anubhava的答案已經解決了這個問題,並提供了一個很好的解決方案。 我想我只是參考您的原始代碼,對您所說的示例發生了什么提供一些補充說明:

例如: https://example.com/about-us : https://example.com/about-ushttps://example.com/index.php

要求提供https://example.com/about-us

  1. 這符合您的第二條規則(HTTPS為“ on”,並請求about-us ),並重定向到http://example.com/about-us (即,返回HTTP)。

  2. 重定向的請求(即http://example.com/about-us )現在與最后一條規則匹配,並在內部被重寫index.php (前端控制器)。

  3. 但是,在每個目錄的.htaccess文件( 目錄上下文)中,“重寫的請求將交還給URL解析引擎”,並且該過程有效地重新開始。 REQUEST_URI服務器變量也被更新以保存重寫的URL,即。 /index.php

  4. 在第二次通過.htaccess文件時,請求(現已重寫為http://example.com/index.php )與您的第一個規則匹配(HTTPS為“ off”且請求不是/about-us/termsofus )因此,該請求(第二次)被重定向https://example.com/index.php (內部重寫實際上已更改外部重定向 。)

  5. 重定向的請求(現在為https://example.com/index.php )與您的.htaccess文件中的任何規則均不匹配,因此將保持不變。 頁面已投放。

如果檢查網絡流量,則應該看到上面提到的兩個外部重定向。

另一種可能的解決方案是在最后一個RewriteRule上使用END標志(僅適用於Apache 2.4+)。 這有效地結束了URL重寫過程,因此該過程在步驟#2處停止。 盡管我仍然會喜歡anubhava的解決方案,並對照THE_REQUEST檢查,但該解決方案在Apache 2.2上有效,並且在您引入其他重寫后仍然可以使用。

暫無
暫無

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

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