HTTP 响应 Header 正在被覆盖。 HTTP Response headers 在apache 哪里可以设置?
HTTP Response Header is being overwritten. Where all can HTTP Response headers be set in apache?
我有一个 PHP 应用程序,我在其中有条件地设置了 Access-Control-Allow-Origin header。我看到更改反映在我的本地设置和开发环境中,但在实时站点上,header 被设置为其他内容。我设置的其他 header 保留了它们的值,所以它让我相信 Access-Control-Allow-Origin header 正在其他地方被覆盖。
我已经检查了我项目中的 .htaccess 文件和 apache 虚拟主机配置文件,以查找 header 可能被覆盖的地方。它是在虚拟主机配置文件中设置的,但我将其注释掉并重新启动了 apache,但 header 仍然被覆盖。
有没有其他地方可以检查 header 是否被覆盖?
在此先感谢您的帮助!
这是请求的 PHP 代码片段:
$origin=$front->getRequest()->getHeader('Origin');
if($origin && (preg_match('/http[s]{0,1}:\/\/' . $front->getRequest()->getHttpHost() . '$/', $origin))){
$front->getResponse()->setHeader('Access-Control-Allow-Origin', $origin);
$front->getResponse()->setHeader('Access-Control-Allow-Credentials', 'true');
}else{
//leave current value if there is no match
$front->getResponse()->setHeader('Access-Control-Allow-Origin', '*');
}
我很确定 header 正在被其他东西覆盖,因为我可以看到 Access-Control-Allow-Credentials:true
按预期通过,但是 Access-Control-Allow-Origin
的值为 *
.
您可以通过 htaccess 设置 header:
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
或来自PHP:
header("access-control-allow-origin: *");
您可以使用:
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|font.css|css)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
为指定文件应用 htaccess header。
我做了更多的挖掘,发现 this link 在 .htaccess 中做同样的事情。我最后添加了以下内容:
SetEnvIf Origin "^http(s)?://(.+\.)?(www.example.com)$" origin_is=[=10=]
Header set Access-Control-Allow-Origin %{origin_is}e env=origin_is
Header set Access-Control-Allow-Credentials true env=origin_is
我有一个 PHP 应用程序,我在其中有条件地设置了 Access-Control-Allow-Origin header。我看到更改反映在我的本地设置和开发环境中,但在实时站点上,header 被设置为其他内容。我设置的其他 header 保留了它们的值,所以它让我相信 Access-Control-Allow-Origin header 正在其他地方被覆盖。
我已经检查了我项目中的 .htaccess 文件和 apache 虚拟主机配置文件,以查找 header 可能被覆盖的地方。它是在虚拟主机配置文件中设置的,但我将其注释掉并重新启动了 apache,但 header 仍然被覆盖。
有没有其他地方可以检查 header 是否被覆盖?
在此先感谢您的帮助!
这是请求的 PHP 代码片段:
$origin=$front->getRequest()->getHeader('Origin');
if($origin && (preg_match('/http[s]{0,1}:\/\/' . $front->getRequest()->getHttpHost() . '$/', $origin))){
$front->getResponse()->setHeader('Access-Control-Allow-Origin', $origin);
$front->getResponse()->setHeader('Access-Control-Allow-Credentials', 'true');
}else{
//leave current value if there is no match
$front->getResponse()->setHeader('Access-Control-Allow-Origin', '*');
}
我很确定 header 正在被其他东西覆盖,因为我可以看到 Access-Control-Allow-Credentials:true
按预期通过,但是 Access-Control-Allow-Origin
的值为 *
.
您可以通过 htaccess 设置 header:
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
或来自PHP:
header("access-control-allow-origin: *");
您可以使用:
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|font.css|css)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
为指定文件应用 htaccess header。
我做了更多的挖掘,发现 this link 在 .htaccess 中做同样的事情。我最后添加了以下内容:
SetEnvIf Origin "^http(s)?://(.+\.)?(www.example.com)$" origin_is=[=10=]
Header set Access-Control-Allow-Origin %{origin_is}e env=origin_is
Header set Access-Control-Allow-Credentials true env=origin_is