大网数据广告活动
服务器运维问答

Break up a long line in a .htaccess file

· 52CE 我爱测

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 …

问题原文

原帖链接 · 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:

  1. Whitespace within a line is fine, ie. any number of tabs and spaces;
  2. The last character on all lines except the final one must be a backslash;<
  3. The final line must not terminate with a backslash;
  4. The Apache comment character (#) cannot be used to comment out a line.
  5. 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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-01-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。