.htaccess标头被Apache忽略

Mar*_*tin 8 php apache .htaccess

我有一个网站使用的核心.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
Run Code Online (Sandbox Code Playgroud)

该网站本身可以很好地加载,但是.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");
?>
Run Code Online (Sandbox Code Playgroud)

.htaccess不会认可中设置的相同标头。

所以这是.htaccess语法错误!

我看不到;通常,网站会显示.htaccess 错误,并会加载HTTP-500错误消息,但是此处,网站在浏览器中的加载不会出现问题。

IS故意语法错误的错误-500 HTTP响应返回预期。

Ok 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>
Run Code Online (Sandbox Code Playgroud)

最后,如果您给我以上所有内容的最终摘要,那将真的有帮助!

  • 标头设置命令.htaccess似乎不起作用。
  • 该文件的所有部分均可在其他地方的其他活动站点上使用,不会出现问题。
  • 标题可以在PHP中设置而不会出现问题
  • 这些标题中没有错误.htaccess
  • 标头似乎无声地失败了。
  • 没有记录Apache错误日志。
  • .htaccess 由Apache的读取,因为其它命令(例如mod_Rewrite多个)正在付诸行动

更新:

从其他方面(托管提供者)的研究来看,似乎可以为非PHP页面.htaccess工作并加载所有正确的标头。

对于普通的PHP页面;标头为空白。

澄清度

  • what.html页面都可以正常加载标头。
  • PHP页面显示标头设置 Header("...");
  • PHP页面拒绝加载任何由设置的标头.htaccess。这就是问题。

因此,看来.htaccess无法为PHP页面设置标题。我怎样才能解决这个问题?

cov*_*ner 7

它与其说是 FastCGI,不如说是 mod_proxy_fcgi,这是一种通过将 FastCGI 传递给其他侦听器来要求 Apache“执行”FastCGI 的方法。

当您使用任何 mod_proxy* 模块时,根本不会处理 .htaccess,因为您充当代理并短路任何与磁盘相关的配置部分。

php-fpm 将查看请求 URL 并从磁盘读取数据,但 Apache 不会。这只是让人们感到困惑,因为他们可以在同一台主机上运行,​​并且文件通常位于 httpd 可以直接提供服务的目录中。


dig*_*jay 6

当作为FastCGI模块工作时,PHP似乎会忽略.htaccess中定义的标头。

关于如何解决此问题,有很多建议。在您的情况下,我建议您使用一个定义所有标头的文件

<?php
// file headers.php
header('Cache-Control: no-cache,must-revalidate');
header('X-Clacks-Overhead: "GNU Terry Pratchett"');
header('X-XSS-Protection: 1;mode=block');
header('X-Content-Type-Options: nosniff');
header('X-Frame-Options: SAMEORIGIN');
header('Expect-CT: enforce,max-age=2592000');
header('Content-Language: en');
header('Referrer-Policy: origin-when-cross-origin');
?>
Run Code Online (Sandbox Code Playgroud)

并将其保存到您的DocumentRoot目录。然后将此条目添加到您的.htaccess文件中,以将其包含在每个请求中:

php_value auto_prepend_file /var/www/html/headers.php     
Run Code Online (Sandbox Code Playgroud)

测试它:

<?php
// file test.php
die("hello world");
?>
Run Code Online (Sandbox Code Playgroud)

标头正在发送:

$ curl -I ubuntu-server.lan/test.php
HTTP/1.1 200 OK
Date: Sun, 25 Nov 2018 09:37:52 GMT
Server: Apache/2.4.18 (Ubuntu)
Cache-Control: no-cache,must-revalidate
X-Clacks-Overhead: "GNU Terry Pratchett"
X-XSS-Protection: 1;mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Expect-CT: enforce,max-age=2592000
Content-Language: en
Referrer-Policy: origin-when-cross-origin
Content-Type: text/html; charset=UTF-8
Run Code Online (Sandbox Code Playgroud)

始终记住,当您更改.htaccess中的标头时,也要同时更改headers.php中的标头。

希望这可以帮助!


?先前的答案

我认为此问题是由于headers_module未正确加载httpd / apache2导致的(尽管您在上述注释之一中另有说明)。您可以通过在终端中执行以下命令来检查:

apachectl -M | grep headers_module
Run Code Online (Sandbox Code Playgroud)

如果没有输出headers_module (shared)(或类似输出),则必须激活httpd / apache2标头模块。在CentOS系统上,您必须在配置中加载相应的源文件(默认/etc/httpd/conf/httpd.conf)。

您必须添加此行

LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so
Run Code Online (Sandbox Code Playgroud)

然后重启http服务器王氏 sudo systemctl restart httpd.service

使用EasyApache 4,httpd / apache2模块所在的文件夹可能会有所不同,并且可能是/usr/lib64/apache2/modules/

我希望这有帮助!


Mar*_*tin 0

经过多次探索,发现问题出在 PHP 处理程序上—— fastCGI(cgi)处理程序没有保留标头。

更改为suphp处理程序立即解决了问题。


归档时间:

查看次数:

1892 次

最近记录:

6 年,11 月 前