
我有一個應用程序,它是多個網域上的伺服器(每個網域都自訂應用程式的外觀。
此外,還有一個公共域,它有多個子域來顯示與上面相同的資料。
所以 -www.apples.com
看起來與apples.efruit.co.uk
和 -www.bananas.co.uk
看起來相同bananas.efruit.co.uk
ETC
現在,我有一個用於 efruit.co.uk 網域的通配符 ssl,因此我想使用 htaccess 檔案強制所有 efruit.co.uk 請求使用 https(如果尚未這樣做)。
所有其他網域都沒有 ssl,因此它們需要使用 http 來避免瀏覽器警告 - 再次來自 htaccess 檔案。
我還想使用 htaccess 文件,這樣如果有人輸入 www.apples.efruit.co.uk ,它會重寫為 apples.efruit.co.uk (透過 https 如上所述)。然而,www.如果不是 efruit.co.uk 網域也可以。
最後,我為 codeigniter 提供了標準且有據可查的重寫規則,以使 url 更漂亮。
這是我到目前為止所擁有的。
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !(health_check\.php)$
RewriteCond %{HTTP_HOST} ^(.*)\.efruit\.co\.uk
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} !^(.*)\.efruit\.co\.uk
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^www\.([^\.]*)\.efruit\.co\.uk$
RewriteRule (.*) https://%1.efruit.co.uk$1 [R,L]
# block hidden directories
RewriteRule "(^|/)\." - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
唯一有效的方法是強制 efruit.co.uk 網域透過 https 提供服務。
答案1
您似乎位於 SSL 代理程式後面(?),因此使用X-Forwarded-Proto
請求標頭,而不是HTTPS
伺服器變數。
RewriteCond %{REQUEST_URI} !(health_check\.php)$ RewriteCond %{HTTP_HOST} ^(.*)\.efruit\.co\.uk RewriteCond %{HTTP:X-Forwarded-Proto} =http RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
雖然您聲明此位元「有效」(「強制efruit.co.uk
透過 https 提供網域」),但這不會重定向主裸域,並且還會維護 www 子網域(如果請求中存在) - 您應該先將其刪除避免二次重定向。因此,需要更改這些指令的順序。
您也不需要捕獲組(即帶括號的子模式),除非您打算將其用作反向引用 - 但您似乎不會這樣做。
所有其他網域都沒有 ssl,因此它們需要使用 http 來避免瀏覽器警告 - 再次來自 htaccess 檔案。
您無法發出 HTTPS 到 HTTP 重定向,.htaccess
以避免在這些其他網域上出現瀏覽器警告 - 這似乎是您所建議的。因為 SSL 憑證無效瀏覽器警告發生前 .htaccess
被執行。 SSL 握手發生在請求的一開始。
RewriteCond %{HTTP_HOST} !^(.*)\.efruit\.co\.uk RewriteCond %{HTTP:X-Forwarded-Proto} =https RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
因此,上述內容實際上不會從瀏覽器執行,除非使用者接受 SSL 憑證警告(他們不應該接受)。
RewriteCond %{HTTP_HOST} ^www\.([^\.]*)\.efruit\.co\.uk$ RewriteRule (.*) https://%1.efruit.co.uk$1 [R,L]
.htaccess
對於除文檔根之外的任何內容,這都會導致無效的重定向,因為反向$1
引用沒有斜線前綴。即使對於文件根目錄,您也應該在其末尾添加一個斜杠代換(儘管瀏覽器會隱式修復此問題)。另外,好奇為什麼REQUEST_URI
在前兩條規則中使用伺服器變量,但在這裡使用反向引用?
請嘗試類似以下內容:
# Prevent any fruther processing when requesting "health_check.php"
# CHECK: Absence of start of string anchor?
RewriteRule health_check\.php$ - [L]
# Remove www sub-subdomain (and redirect to HTTPS)
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.efruit\.co\.uk [NC]
RewriteRule .* https://%1.efruit.co.uk%{REQUEST_URI} [R,L]
# HTTP to HTTPS redirect for the "efruit.co.uk" domain (and subdomains)
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteCond %{HTTP_HOST} ^([^.]+\.)?efruit\.co\.uk [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# HTTPS to HTTP redirect for none "efruit.co.uk" domains
# Note that the user will still have to click through any browser security warnings
RewriteCond %{HTTP_HOST} !(^|\.)efruit\.co\.uk
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
其他注意事項:
- 在字元類別中使用時,無需反斜線轉義文字點。
- 我已刪除
$
主機檢查上的字串錨點末尾,以便捕獲包含尾隨點的 FQDN。 - 一旦您確認這些永久 (301) 重定向工作正常,您可能會想要更改它們。 IE。改成。
R
R=301