问题原文
原帖链接 · MaxPRafferty · CC BY-SA 3.0
I am setting a Content-Security-Policy header in my .htaccess file, and it has grown to be an extremely long single line, which is a bother to manage. Is there some way to break up this line into more manageable substrings?
As a trivial example, say I am setting a header like
Header set Content-Security-Policy "deafult-src http://domainA.com; script-src http://domainB.com"
I can (without obvious breaking problems) accomplish my specific case with something like
Header append Content-Security-Policy "default-src http://domainA.com;"
Header append Content-Security-Policy "script-src http://domainB.com"
but that will insert commas into the string, so I was still curious if there was a better answer that could be applied generally, without adding the additional chars to the response.
What would be ideal is if there were some concatenation character that I could use to break the string into smaller parts, like
Header set Content-Security-Policy "default-src http://domainA.com;"
\" script-src http://domainB.com"
or
Header set Content-Security-Policy "default-src http://domainA.com;"^
" script-src http://domainB.com"
or
Header set Content-Security-Policy "default-src http://domainA.com;"
+" script-src http://domainB.com"
Alternatively, if I could set some sort of variable and just dump their contents to do something like
a="default-src http://domainA.com;"
b=" script-src http://domainB.com"
Header set Content-Security-Policy $a$b
that would also be much more managable.
There was a similar topic that came up for nginx and the conclusion was just to live with the long lines (they were dealing with a long regex, so the append solution wouldn't have worked); Is that going to be the case for Apache as well?
社区高分解答
原帖链接 · Steve GS · CC BY-SA 4.0
Yes - the backslash works as a line-continuation. This is buried within the Apache 2.4 documentation on https://httpd.apache.org/docs/2.4/configuring.html#page-header
Important rules:
- Whitespace within a line is fine, ie. any number of tabs and spaces;
- The last character on all lines except the final one must be a backslash;<
- The final line must not terminate with a backslash;
- The Apache comment character (#) cannot be used to comment out a line.
- You can not break the [flags]
If these rules are not obeyed, the server will respond with an Error 500.
Note that when editing *.conf files you can use C:\Apache24\bin>httpd -t to check your syntax.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
