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
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响应返回预期。
绝对; 我完全同意。Apache错误日志为空!
httpd.conf允许阅读.htaccess.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似乎不起作用。.htaccess。.htaccess 是由Apache的读取,因为其它命令(例如mod_Rewrite多个)被正在付诸行动从其他方面(托管提供者)的研究来看,似乎可以为非PHP页面.htaccess工作并加载所有正确的标头。
对于普通的PHP页面;标头为空白。
澄清度
Header("..."); .htaccess。这就是问题。因此,看来我
.htaccess无法为PHP页面设置标题。我怎样才能解决这个问题?
它与其说是 FastCGI,不如说是 mod_proxy_fcgi,这是一种通过将 FastCGI 传递给其他侦听器来要求 Apache“执行”FastCGI 的方法。
当您使用任何 mod_proxy* 模块时,根本不会处理 .htaccess,因为您充当代理并短路任何与磁盘相关的配置部分。
php-fpm 将查看请求 URL 并从磁盘读取数据,但 Apache 不会。这只是让人们感到困惑,因为他们可以在同一台主机上运行,并且文件通常位于 httpd 可以直接提供服务的目录中。
当作为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/。
我希望这有帮助!
| 归档时间: |
|
| 查看次数: |
1892 次 |
| 最近记录: |