15

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.

Community
  • 1
  • 1
Oriol
  • 274,082
  • 63
  • 437
  • 513

2 Answers2

13

It looks like improving this is still under construction as of Apache 2.4. Apache team member "covener" recommends m#regexp# instead.

So your code would look like this...

<If "%{HTTP:Accept} !~ m#application/xhtml\+xml#">
Simon East
  • 55,742
  • 17
  • 139
  • 133
lkraav
  • 2,696
  • 3
  • 26
  • 26
  • 2
    I can't see that comment_3455 by Covener. But the [Other](http://httpd.apache.org/docs/2.4/expr.html#other) section says `m#regexp#` is an alternative of `/regexp/`. – Oriol May 31 '15 at 19:15
  • Strange, the anchor shoots me directly to the correct discussion reply. But also great find on the official Other documentation table. – lkraav May 31 '15 at 19:32
  • 1
    `m#regexp#` works well for me when evaluating against `REQUEST_URI`, which will have forward slashes in a typical scenario. – RjOllos Jun 07 '15 at 07:31
  • This answer is correct. But there are indeed no comments onthe linked page at all. And for sure not a comment by some Apache developer. – feeela Feb 28 '22 at 10:18
  • Yeah, comments section seems to be gone. Can't even find it archive.org snapshots. I'm fairly certain I didn't just invent `#comment_3455` anchor link at the time, Apache docs used to have a commenting section for sure. – lkraav Mar 02 '22 at 07:55
12

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

Qtax
  • 33,241
  • 9
  • 83
  • 121