我有這個規則可以將所有 http 連接切換到 https。編輯以顯示整個檔案
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.kiwi$ [OR]
RewriteCond %{HTTP_HOST} ^example\.nz$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.nz$
RewriteRule ^/?$ "https\:\/\/example\.kiwi\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.app$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.app$
RewriteRule ^/?$ "https\:\/\/example\.kiwi\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.online$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.online$
RewriteRule ^/?$ "https\:\/\/example\.kiwi\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.software$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.software$
RewriteRule ^/?$ "https\:\/\/example\.kiwi\/$1" [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
Header always setifempty Strict-Transport-Security "max-age=31536000" env=HTTPS
Header append X-Frame-Options: "SAMEORIGIN"
不幸的是,它會影響諸如 mail.domain.com(和其他)之類的東西。如何阻止任何/所有子域升級?
uj5u.com熱心網友回復:
假設您只想將 www 子域和域頂點重定向到 HTTPS,那么您可以檢查指令中的Host標頭RewriteCond。
例如:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
以上只會重定向www.example.com或example.com。
我已經洗掉了RewriteRule 模式(即^(.*)$)中的捕獲組,因為這里沒有使用它。
使用 302(臨時)重定向進行測驗以避免快取問題。
您需要在測驗前清除瀏覽器快取。
還要考慮在重定向中規范化主機名(www 與非 www)。
Header always set Content-Security-Policy "upgrade-insecure-requests;"
但是,由于現在并非所有內容都是 HTTPS,如果您通過 HTTP 從子域加載資源,則應根據請求是否通過 HTTPS 開始有條件地發送此標頭。
例如,在 Apache 2.4.10 上,您可以在Header指令中使用 Apache 運算式語法:
Header always set Content-Security-Policy "upgrade-insecure-requests;" "expr=%{HTTPS} == 'on'"
更新:
我希望使用不同的后綴,例如 example.software 和 example.app,所以我應該將“.com”替換為“.?”
您可以在第一個條件下簡單地洗掉正則運算式中的 TLD 。例如:
RewriteCond %{HTTP_HOST} ^(www\.)?example\. [NC]
現在,這將匹配example.com,example.software,www.example.app等等。但是,如果你碰巧有一個子域相同的名稱作為域名本身它會失敗(例如example.example.com) -但應避免。
我已經編輯了帖子以顯示我的整個檔案,以防出現副作用。我有許多域在構建網站時都被重定向了。
這不會影響 HTTP 到 HTTPS 的重定向。這些指令將所有其他域重定向到https://example.kiwi- 因此,當處理這些指令時,無論如何都會繞過(不需要)稍后的 HTTP 到 HTTPS 重定向。
但是,這些重定向存在一些問題。
- “暫時重定向” - 您暗示這些重定向是臨時的,但您使用的是 301(永久)回應。這會導致重定向被永久快取,這可能無法撤消。如果這是臨時的,那么它應該是 302(臨時)重定向。
^/?$- You are only redirecting requests for the document root (ie. the homepage). A request forexample.nz/foowill not be redirected. This further looks like an error because you are using a$1backreference in the substitution string, which will consequently always be empty.
These redirect directives could also be greatly simplified. If you are "temporarily" redirecting all other domains to https://example.kiwi/<url> then you could do it with a single rule like this instead:
RewriteCond %{HTTP_HOST} !=example.kiwi
RewriteRule ^ https://example.kiwi%{REQUEST_URI} [R=302,L]
Where !=example.kiwi is a negated exact match string comparison (not a regex). The condition is successful then the Host header does not match example.kiwi.
Aside:
Header always setifempty Strict-Transport-Security "max-age=31536000" env=HTTPS
You shouldn't be implementing HSTS whilst still in development and changing domains. However, you don't appear to be setting an HTTPS env var in the code you have posted, so this is likely being ignored anyway. (?)
NB: env=HTTPS is not checking the server variable of the same name.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/322365.html
