【发布时间】:2020-12-02 15:14:43
【问题描述】:
我的 Wordpress 网站通过 .htaccess 文件设置了 HSTS 标头,它工作正常,直到某个时候它停止发送标头,我不知道何时或为什么(我一直在配置和更新我的VPS 服务器,所以一切皆有可能)。问题是,如果我将标头直接添加到 httpd.conf 文件,它仍然有效,只是当我尝试通过 .htaccess 文件逐个站点进行操作时,它仍然有效。使用 .htaccess 顺便说一句,重定向、密码和其他东西仍然有效...
此外,我创建的空白测试 html 页面仍然发送 HSTS 标头。那么,在提供 wordpress PHP 页面时,什么会覆盖 HSTS 规则呢?因为显然 httaccess 代码仍然是正确的,因为 html 页面按预期提供了它。
这是我使用的代码:
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" env=HTTPS
我再说一遍,如果我编辑 Apache 配置文件,同样的代码可以工作......当我提供空白 html 文件时,它仍然可以通过 .htaccess 工作。必须有一个简单的解释?谢谢!
根据 MrWhite 的要求更新。这是我的 .htaccess 的内容:
RewriteEngine On
Header set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" env=HTTPS
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<files wp-config.php>
order allow,deny
deny from all
</files>
Options -Indexes
ErrorDocument 401 default
ErrorDocument 403 Denied
<FilesMatch “wp-login.php”=””>
AuthType Basic
AuthName "Private area. Failed attempt IPs logged and reported."
AuthUserFile "/home/censoredusername/.htpasswds/public_html/wp-admin/passwd"
Require valid-user
</FilesMatch>
<Files admin-ajax.php>
Order allow,deny
Allow from all
Satisfy any
</Files>
# 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
# Wordfence WAF
<Files ".user.ini">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</Files>
# END Wordfence WAF
以下是 WordPress 之外的 html 和 php 附带的重定向和服务器响应(没有指定 htaccess 的 env var)。他们错过了 www 重定向,因为到目前为止该重定向是由 WordPress 完成的。从现在开始会将其包含在 htaccess 中。
PHP:
censored.com/test.php
HTTP/1.1 301 Moved Permanently
Date: Thu, 03 Dec 2020 13:39:33 GMT
Server: Apache
X-XSS-Protection: 1; mode=block;
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Location: https://censored.com/test.php
Content-Length: 243
Content-Type: text/html; charset=iso-8859-1
https://censored.com/test.php
HTTP/1.1 200 OK
Date: Thu, 03 Dec 2020 13:39:36 GMT
Server: Apache
X-XSS-Protection: 1; mode=block;
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Vary: User-Agent
Content-Length: 11
Content-Type: text/html; charset=UTF-8
HTML:
censored.com/test.html
HTTP/1.1 301 Moved Permanently
Date: Thu, 03 Dec 2020 13:42:39 GMT
Server: Apache
X-XSS-Protection: 1; mode=block;
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Location: https://censored.com/test.html
Content-Length: 244
Content-Type: text/html; charset=iso-8859-1
https://censored.com/test.html
HTTP/1.1 200 OK
Date: Thu, 03 Dec 2020 13:42:41 GMT
Server: Apache
X-XSS-Protection: 1; mode=block;
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Last-Modified: Wed, 02 Dec 2020 12:45:56 GMT
Accept-Ranges: bytes
Content-Length: 120
Vary: Accept-Encoding,User-Agent
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
Content-Type: text/html
【问题讨论】:
-
你在哪里设置
HTTPS环境变量? -
“如果我将标头直接添加到 httpd.conf 文件,它仍然有效” - 是使用您在上面发布的 exact 指令吗?虽然它不应该包含
env=参数(因为我假设您只是在 vHost 中为端口 443 设置它)。 -
是的,那是使用完全相同的指令,包括 env=https 参数,因为我猜我不太清楚。我在 WHM/CPanel CentOS 7 VPS 上,所以我不需要手动设置太多就可以让它工作。直到它停止是:/