I am configuring a lightweight content negotiation mechanism using Apaches's Mod-Rewrite. The configuration should deliver several different data representations (for instance, HTML, XML, RDF/XML, and RSS, although I actually have to consider a few more in my application) on a base resource URL depending on the Accept header of the request:

# serve html on base url if requested via accept header
RewriteCond %{HTTP_ACCEPT} text/html [OR]
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml
RewriteRule ^resource/(.*)$ view/html/$1.html [NC,R=303,L]

# serve xml on base url if requested via accept header
RewriteCond %{HTTP_ACCEPT} application/xml
RewriteRule ^resource/(.*)$ view/xml/$1.xml [NC,R=303,L]

# serve rdf on base url if requested via accept header
RewriteCond %{HTTP_ACCEPT} application/rdf\+xml
RewriteRule ^resource/(.*)$ view/rdf/$1.rdf [NC,R=303,L]

# serve rss on base url if requested via accept header
RewriteCond %{HTTP_ACCEPT} application/rss\+xml
RewriteRule ^resource/(.*)$ view/rss/$1.rss [NC,R=303,L]

# serve html as default response (keep at bottom)
RewriteRule ^resource/(.*)$ view/html/$1.html [NC,R=303,L]

This works pretty fine if the request is sending pure Accept headers, but I run into some trouble when mixed Accept headers are sent. In that case, my configuration does not respect any given q-value in the header, and I end up serving the first matching content type according to the (arbitrary) ordering of my RewriteRules. E.g., I incorrectly serve text/html for the following request:

Accept: application/rdf+xml;q=0.5,text/html;q=.3

Is there any way how I can make my configuration take the q-values of the Accept header into account? Any help is appreciated.

  • 1
    Would you be able to use server-side scripting (e.g., PHP, Python, etc.)? I’m looking for a .htaccess solution and haven’t found a solution yet: Redirect depending on preferred language (Accept-Language) with .htaccess
    – unor
    Feb 5, 2015 at 11:03
  • @unor: When nothing else is possible, I guess I could use server sided scripting, but in my case this would require a fair amount of tweaking the established infrastructure. I would be way more happy if there were an Apache configuration solution.
    – MRA
    Feb 5, 2015 at 12:53

1 Answer 1


Use the built-in content negotiation functionality with a type map. You may need to tweak your filenames / URLs or use rewrite rules after applying the type map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.