简体   繁体   English

PHP 标头函数会覆盖 htaccess 标头吗?

[英]does PHP header function overwrite htaccess headers?

Setting headers in PHP在 PHP 中设置标题

header("Access-Control-Allow-Origin: https://www.example.com");
header("Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description");
    

Header set in .htaccess .htaccess中设置的标头

SetEnvIf Origin ^(https?://.+\.(example|gstatic)\.com(?::\d{1,5})?)$   CORS_ALLOW_ORIGIN=$1
Header append Access-Control-Allow-Origin  %{CORS_ALLOW_ORIGIN}e   env=CORS_ALLOW_ORIGIN

Header set Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header set Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

Header merge  Vary "Origin"

I'd rather not edit the .htaccess file.我宁愿不编辑.htaccess文件。 Does the PHP header() function overwrite? PHP header()函数是否覆盖?

Without editing your .htaccess , it's going to be difficult, but if you allow yourself a small edit, you can make it work.如果不编辑您的.htaccess ,将会很困难,但是如果您允许自己进行一些小编辑,您就可以让它工作。

The Header Apache2 documentation says: Header Apache2 文档说:

This directive can replace, merge or remove HTTP response headers.该指令可以替换、合并或删除 HTTP 响应标头。 The header is modified just after the content handler and output filters are run, allowing outgoing headers to be modified.标题在内容处理程序和输出过滤器运行后立即修改,允许修改传出标题。

So it happens after PHP (aka content handler ).所以它发生在 PHP(又名内容处理程序)之后。

However, if we look at the syntax of the option:但是,如果我们查看选项的语法:

Header [condition] add|append|echo|edit|edit*|merge|set|setifempty|unset|note header [[expr=]value [replacement] [early|env=[!]varname|expr=expression]]

we see that one of the actions is setifempty .我们看到其中一个动作是setifempty This is what you actually want to use.这是你真正想要使用的。 The set overwrites the value.set会覆盖该值。 setifempty sets the value if not already set. setifempty设置值(如果尚未设置)。

Header setifempty Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header setifempty Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

Here is that option description:这是该选项的说明:

setifempty
The request header is set, but only if there is no previous header with this name.请求标头已设置,但前提是之前没有具有此名称的标头。

Note:笔记:

There is a comment about using the header_remove() PHP instruction.有一条关于使用header_remove() PHP 指令的评论。 Only that happens at the time PHP runs which is before the Apache2 Header is applied.只有在应用 Apache2 Header之前 PHP 运行时才会发生这种情况。 So it wouldn't work.所以它不会工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM