Search code examples
regexapacheif-statementescapingslash

How can I escape slash sign `/` in Apache `<If>` directive's regex?


I wanted to serve .xhtml files as

  • application/xhtml+xml if the browser says that it accepts it.
  • text/html otherwise

I tried doing it with mod_rewrite but it didn't work with Options -FollowSymLinks (see Why I do I get 403 Forbidden when viewing files affected by Apache RewriteRule if I have `Options -FollowSymLinks`?).

Then, I tried

<Files "*.xhtml">
    <If "%{HTTP:Accept} !~ /application\/xhtml\+xml/">
        ForceType text/html
    </If>
</Files>

But I get a syntax error: Failed to compile regular expression.

Meanwhile, I use this code...

<Files "*.xhtml">
    <If "%{HTTP:Accept} !~ /xhtml\+xml/">
        ForceType text/html
    </If>
</Files>

... which works, but I want to match the correct MIME type.


Solution

  • You could use an escape code like \x2F instead of the /.