【发布时间】:2019-04-15 01:05:11
【问题描述】:
我有一个网站使用与许多其他网站相同的核心 .htaccess 细节;但是这个网站没有正确加载.htaccess 指令——给出一个基本的 HTTP 标头集:
HTTP/1.1 200 OK
Date: Mon, 12 Nov 2018 09:34:28 GMT
Server: Apache
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
网站本身可以正常加载,但 .htaccess 中的附加标头没有被识别/加载。
所以.htaccess 正在被读取,对吧?
是 -- htaccess 文件包含 HTTPS 强制重定向和域名重定向(从 .co.uk 到 .com 地址(都到同一个网站帐户))
这些工作。
PHP 提供的头文件也可以正常加载
测试页面上的 PHP 标头加载正常:
<?php
header("Cache-Control: no-cache, must-revalidate");
header('Content-Type: text/html; charset=utf-8');
header("X-Clacks-Overhead: GNU Terry Pratchett");
header("Content-Language: en");
header("X-XSS-Protection: 1; mode=block");
header("X-Frame-Options: SAMEORIGIN");
header("X-Content-Type-Options: nosniff");
?>
但.htaccess 中设置的相同标头没有被识别。
所以这是一个.htaccess 语法错误!
我看不到;通常带有 .htaccess error 的站点会加载 HTTP-500 错误消息,但是这里的站点在浏览器中加载没有问题。
当出现IS故意的语法错误时,错误 500 HTTP 响应会按预期返回。
好的 bozo,检查你的错误日志!
绝对;我完全同意。 Apache 错误日志为空!
您尝试过什么来解决这个问题?
- 确认
httpd.conf允许读取.htaccess - 确认 mod_headers.c 已加载到服务器上
- 注释掉并重写了各种规则,没有效果
- 阅读 Stack Overflow 和 Server Fault 上的大量帖子(可能是 6 到 8 条) - Stackoverflow 帖子似乎不相关,或者它们的问题有明显差异。
- 确认我的
.htaccess具有正确的许可 (0644) - 告诉我的员工(他是一名平面设计师)。
- 哭着睡着了。
那么——把你的文件拿出来!让我看看魔法!
这里:
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
ErrorDocument 404 /index.php?msg=404
ErrorDocument 403 /index.php?msg=403
#Set asset items to cache for 1 week.
<FilesMatch "\.(gif|jpe?g|png|ico|css|js|swf|mp3)$">
Header set Cache-Control "max-age=1972800, public, must-revalidate"
</FilesMatch>
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
## This does not appear to work (for either)
#Header always set Strict-Transport-Security "max-age=31536000;" env=HTTPS
Header always set Strict-Transport-Security "max-age=31536000; includeSubdomains;" "expr=%{HTTPS} == 'on'"
Header set Expect-CT enforce,max-age=2592000
RewriteCond %{HTTP_HOST} ^(www\.)?thewebsite\.co\.uk$ [NC]
RewriteRule ^/?(.*)$ https://www.thewebsite.com%{REQUEST_URI} [R=301,L]
###
##### Seems to workdown to roughly this point.
###
#force requests to begin with a slash.
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_URI} !^/
RewriteRule .* - [R=403,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
### This file does not exist on the directory at present.
<Files .account-user.ini>
order allow,deny
deny from all
</Files>
###
#### None of these appear on assessment tools such as Security Headers
#### Or redbot.
###
Header set Cache-Control no-cache,must-revalidate
Header set X-Clacks-Overhead "GNU Terry Pratchett"
Header set X-XSS-Protection 1;mode=block
Header set X-Content-Type-Options nosniff
Header always set X-Frame-Options SAMEORIGIN
Header set Expect-CT enforce,max-age=2592000
Header set Content-Language en
Header set Referrer-Policy origin-when-cross-origin
<LimitExcept GET POST HEAD>
deny from all
</LimitExcept>
最后,如果你能对以上所有内容进行最后总结,那真的很有帮助!
-
.htaccess中的标头设置命令似乎不起作用。 - 文件的所有部分在其他地方的其他实时站点上使用,没有问题。
- 可以在 PHP 中毫无问题地设置标题
-
.htaccess中的这些标头没有出现错误。 - 标头似乎静默失败。
- 没有记录 Apache 错误日志。
-
.htaccess正在被 Apache 读取,因为其他命令(例如mod_Rewrites)正在被执行
更新:
根据其他方(托管服务提供商)的研究,.htaccess 似乎以某种方式工作并为非 PHP 页面加载所有正确的标头。
即使是普通的 PHP 页面;标题是空白的。
澄清
- whatever.html 页面加载标题一切正常。
- PHP 页面显示由
Header("...");设置的标题 - PHP 页面拒绝加载由
.htaccess设置的任何标题。这就是问题所在。
所以它看起来我的
.htaccess不能为PHP 页面设置标题。我该如何解决这个问题?
【问题讨论】:
-
您尝试过 AllowOverride All 吗? httpd.apache.org/docs/2.4/en/mod/core.html#allowoverride
-
@D.Joe 如果允许覆盖关闭,那么 htaccess 中的其他命令也会失败。如前所述——HTTPS 和
www.命令按预期工作。 -
投反对票的原因有什么线索吗?
-
@D.Joe 我已更新问题以更好地突出这一点。
-
您是否偶然将 PHP 作为 CGI 运行?如果是这样,我自己也有同样的问题:Apache won't set headers for PHP script.