
我正在嘗試在我的 WordPress 網站上實施 HSTS(HTTP 嚴格傳輸安全),但沒有任何成功。首先,我設法將我的網站從非 www 重定向到 www ,並包括 https:// ,但我收到了訊息https://hstspreload.org/它應該首先重定向到 www。
我試圖使用 VirtualHosts 配置文件,但沒有成功。所以我做了一些谷歌搜尋並發現這個連結這看起來像是 htaccess 的解決方案,但我仍然遇到問題。如果有人知道如何透過 VirtualHost / Apache 設定檔來實現這一點,那就太好了。
錯誤:HTTP 首先重定向到 www
http://inter.net
https://inter.net
在新增 www 子網域之前,(HTTP) 應立即重新導向至(HTTPS)。現在,第一個重定向是https://www.inter.net/
.需要額外的重定向來確保任何支援 HSTS 的瀏覽器都會記錄頂級網域的 HSTS 條目,而不僅僅是子網域。
我的 htaccess 如下:
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
#### This is what I added : From https://www.danielmorell.com/guides/htaccess-seo/redirects/https-www-and-trailing-slash
#### Force HTTPS://WWW and remove trailing / from files ####
## Turn on rewrite engine
RewriteEngine on
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://www.inter.net/%1 [R=301,L]
# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ https://www.inter.net/$1/ [R=301,L]
# Force HTTPS and WWW
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://www.inter.net/$1 [R=301,L]
# Yoast SEO - XML Sitemap Rewrite Fix
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap_index.xml$ /index.php?sitemap=1 [L]
RewriteRule ^locations.kml$ /index.php?sitemap=wpseo_local_kml [L]
RewriteRule ^geo_sitemap.xml$ /index.php?sitemap=geo [L]
RewriteRule ^([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 [L]
RewriteRule ^([a-z]+)?-?sitemap.xsl$ /index.php?yoast-sitemap-xsl=$1 [L]
# END Yoast SEO - XML Sitemap Rewrite Fix
ps - inter.net 網址僅作為範例。
編輯 - 我已經編輯了我的 example.com.conf 文件,以在下面的答案中添加給定我的 MrWhite 的額外規則 - 這看起來很準確。運行指令 apachectl configtest
Syntaw後就OK了。執行service apache2 reload
變更以使變更生效,並讓所有瀏覽器都說頁面未正確重定向:(**ERR_TOO_MANY_REDIRECTS**
每次為每個不同的瀏覽器清除快取)。
我只將 htaccess 還原為原始的 Wordpress 和 Yoast SEO 規則。
我目前在 apache 上針對此 VirtualHost 的設定檔可能有問題,但 apachectl configtest 沒有語法錯誤:https://paste.ofcode.org/vr25hFkPEt2vYjpM5sAUxK
我嘗試使用 Firefox Developer 模組 (F12) 來查看是否可以理解任何其他信息,問題似乎是 301 重定向循環https://www.example.com
編輯2:感謝@MrWhite,我明白ServerAlias
細節是不必要的,並且是循環的原因。問題解決了並從中吸取了教訓。
答案1
概括起來,HSTS的主要要求是:
從 HTTP 重新導向到 HTTPS在同一主機上。 IE。
http://example.com
到https://example.com
和http://www.example.com
到https://www.example.com
僅在 HTTPS 上重定向到規範主機名稱(www 或非 www)。 (即上面#1 之後)
Strict-Transport-Security
僅在使用 HTTPS 時傳送(STS) HTTP 回應標頭。包括規範重定向(上面#2)。(儘管有幾個消息來源指出 STS 標頭應該僅有的透過 HTTPS 發送,甚至透過純 HTTP 發送完全無效,我不相信情況是這樣。規格聲明 UA 在透過 HTTP 發送時應該簡單地忽略此標頭,因此透過 HTTP 發送此標頭也不是「問題」。但是,僅透過 HTTPS 發送此內容並不需要太多工作,這就是我在下面實現的方式。
因此,這意味著您不一定能夠在單一重定向中規範化請求(HTTP / HTTPS / www / 非 www),因為這可能違反上面的#1。
您似乎也沒有在您發布的程式碼中設定 STS 標頭。如果您在 Apache(伺服器配置或)中實現重定向,.htaccess
那麼您無法使用 WordPress 設定此標頭 - 如果這就是您正在做的事情?
我做了一些谷歌搜索,發現這個連結看起來像是 htaccess 的解決方案
該「解決方案」不是實施 HSTS。該文章的唯一目的是在單一重定向中規範化請求。該文章頂部的「警告」明確告訴您它違反了 HSTS。
您也以錯誤的順序放置了指令。這些「重定向」指令需要去前WordPress 前端控制器,否則根本無法為 WordPress 進行處理虛擬的網址。
我假設您的規範主機名稱是www.example.com
. (雖然您在問題標題中提到重定向到非 www,但您在問題的其餘部分中都重定向到 www?)
我試圖使用 VirtualHosts 配置文件,但沒有成功。
儘管在伺服器配置中實現這一點可以說更簡單、更不容易出錯並且更有效(使用單獨的虛擬主機)。
例如(省略「其他」必要指令):
<VirtualHost *:80>
ServerName example.com
# Redirect to HTTPS - same host
Redirect 301 / https://example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
# Redirect to HTTPS - same host
Redirect 301 / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
# Redirect to canonical host (HTTPS only)
Redirect 301 / https://www.example.com/
# SSL directives...
# Set STS header on the HTTPS redirect ("always" argument is required for this)
Header always set Strict-Transport-Security "max-age=2592000; includeSubDomains"
</VirtualHost>
<VirtualHost *:443>
# Canonical host
ServerName www.example.com
# SSL directives...
# etc.
# Set STS header on the HTTPS response
Header always set Strict-Transport-Security "max-age=2592000; includeSubDomains"
</VirtualHost>
注意,上面的STS頭只設定了max-age
1個月的參數,並不包含該preload
參數。如果有意這樣做,請務必遵循 HSTS 預先載入清單的「部署要求」中給出的說明。https://hstspreload.org/#deployment-recommendations
或者,以實現這一點.htaccess
(注意:我還沒有實現“尾部斜杠”重定向,因為您沒有在您的要求中提到這一點,並且它只是從外部文章複製的程式碼的一部分。)
# Set HSTS env var only if HTTPS
RewriteCond %{HTTPS} on
RewriteRule ^ - [E=HSTS:1]
# Redirect HTTP to HTTPS on the same host
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect non-www to www (HTTPS only)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Set HSTS header conditionally if request is over HTTPS only (based on HSTS env var)
Header always set Strict-Transport-Security "max-age=2592000; includeSubDomains" env=HSTS
# Yoast SEO - XML Sitemap Rewrite Fix
# : (directives go here...)
# END Yoast SEO - XML Sitemap Rewrite Fix
# BEGIN WordPress
# : (directives go here...)
# END WordPress
此指令always
需要條件,Header
因此在非 200 OK 回應上設定標頭。 IE。需要在非 www 到 www HTTPS 301 重定向上進行設定。
也可以看看我的答案關於 CodeReview SE 上有關 HSTS 實施的以下問題.htaccess
: