如何在Apache` <If>`指令的正则表达式中转义斜杠符号`/`?

Ori*_*iol 8 regex apache if-statement escaping slash

我想将.xhtml文件作为

  • application/xhtml+xml 如果浏览器说它接受了它.
  • text/html 除此以外

我尝试过这样做mod_rewrite但它不起作用Options -FollowSymLinks(如果我有'Options -FollowSymLinks`,那么在查看受Apache RewriteRule影响的文件时,为什么我会得到403 Forbidden?).

然后,我试过了

<Files "*.xhtml">
    <If "%{HTTP:Accept} !~ /application\/xhtml\+xml/">
        ForceType text/html
    </If>
</Files>
Run Code Online (Sandbox Code Playgroud)

但我得到一个语法错误:无法编译正则表达式.

同时,我用这个代码......

<Files "*.xhtml">
    <If "%{HTTP:Accept} !~ /xhtml\+xml/">
        ForceType text/html
    </If>
</Files>
Run Code Online (Sandbox Code Playgroud)

...有效,但我想匹配正确的MIME类型.

Qta*_*tax 10

您可以使用转义码\x2F而不是/.


lkr*_*aav 5

从Apache 2.4开始,似乎仍在构建中.相反,Apache团队成员"covener" 建议 m#regexp#.

所以你的代码看起来像这样......

<If "%{HTTP:Accept} !~ m#application/xhtml\+xml#">
Run Code Online (Sandbox Code Playgroud)

  • 我看不到 Covener 的 comment_3455。但是 [Other](http://httpd.apache.org/docs/2.4/expr.html#other) 部分说 `m#regexp#` 是 `/regexp/` 的替代品。 (2认同)