Using Apache Httpd mod_rewrite to rewrite URL based on Accept request header?

1

I'm attempting to rewrite a URL based on the Accept header of the request. I saw in the docs that this seems to be possible, but I couldn't find any details or examples anywhere on how to go about it.

Basically I'd like requests to /abc/index.html be rewritten according to the Accept header:

Requests with Accept: application/vnd.x.v1+json to be rewritten as /v1/abc/index.html Requests with Accept: application/vnd.x.v2+json to be rewritten as /v2/abc/index.html

and so forth. And requests with the generic application/json Accept header or no Accept header at all to be rewritten as /v5/abc/index.html

Any pointers on who I could accomplish this are greatly appreciated!

Thanks!

Edy Bourne

Posted 2014-12-30T21:55:21.767

Reputation: 963

Answers

1

RewriteCond %{HTTP_ACCEPT} ^application/vnd\.x\.(v[1-9])\+json
RewriteRule ^(/abc/index\.html)$ /%1/$1 [L]

The RewriteCond line matches the Accept: header and captures the vX string.

The RewriteRule does the actual rewriting and prepends the RewriteCond captured string to the now captured location; using % interpolates RewriteCond captures, $ interpolates RewriteRule captures.

Adjust patterns etc. to suit.

wurtel

Posted 2014-12-30T21:55:21.767

Reputation: 1 359

This worked perfectly! I'll study your example to learn this and derive other uses. Thank you!! – Edy Bourne – 2014-12-31T15:12:52.700