簡體   English   中英

.htaccess 標頭被 Apache 忽略

[英].htaccess headers being ignored by Apache

我有一個網站,它使用與許多其他網站相同的核心.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錯誤,該站點會加載 HTTP-500 錯誤消息,但此處該站點在瀏覽器中加載時沒有問題。

故意語法錯誤的錯誤-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_Rewrite S)正在付諸行動

更新:

從其他方(托管服務提供商)的研究來看, .htaccess似乎以某種方式工作並加載了非 PHP 頁面的所有正確標頭。

即使是普通的 PHP 頁面; 標題是空白的。

澄清

  • what.html 頁面加載標題一切正常。
  • PHP 頁面顯示由Header("...");設置的Header("...");
  • PHP 頁面拒絕加載.htaccess設置的任何標頭。 這就是問題。

所以看起來我的.htaccess無法為 PHP 頁面設置標題。 我怎樣才能解決這個問題?

當作為 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');
?>

並將其保存到您的 DocumentRoot 目錄。 然后將此條目添加到您的 .htaccess 文件以將其包含在每個請求中:

php_value auto_prepend_file /var/www/html/headers.php     

測試它:

<?php
// file test.php
die("hello world");
?>

並且正在發送標頭:

$ 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

請始終記住,當您更改 .htaccess 中的標題時,也要同時更改 headers.php 中的標題。

希望這可以幫助!


➥ 上一個答案

我認為此問題是由於 httpd/apache2 headers_module未正確加載headers_module (盡管您在上述評論之一中另有說明)。 您可以通過在終端中執行此命令來檢查這一點:

apachectl -M | grep headers_module

如果沒有輸出headers_module (shared) (或類似的),那么你必須激活 httpd/apache2 headers 模塊。 在 CentOS 系統上,您必須在配置中加載相應的源文件(默認為/etc/httpd/conf/httpd.conf )。

你必須添加這一行

LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so

然后用sudo systemctl restart httpd.service重啟 http 服務器

使用 EasyApache 4,httpd/apache2 模塊所在的文件夾可能不同,為/usr/lib64/apache2/modules/

我希望這有幫助!

它與其說是 FastCGI,不如說是 mod_proxy_fcgi,這是一種通過將 FastCGI 傳遞給其他偵聽器來要求 Apache“執行”FastCGI 的方法。

當您使用任何 mod_proxy* 模塊時,根本不會處理 .htaccess,因為您充當代理並短路任何與磁盤相關的配置部分。

php-fpm 將查看請求 URL 並從磁盤讀取數據,但 Apache 不會。 這只是讓人們感到困惑,因為他們可以在同一台主機上運行,​​並且文件通常位於 httpd 可以直接提供服務的目錄中。

經過多次探索,發現問題在於 PHP 處理程序fastCGI (cgi) 處理程序沒有保留標題。

更改為suphp處理程序立即解決了問題。

我有同樣的問題。 請在 Linux Ubuntu 中啟用cache模塊。

sudo a2enmod cache

然后運行:

sudo service apache2 start

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM