Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mod_rewrite based on http-header content-type

is it possible to redirect the user, based on the HTTP-Header content-type?

For example:

1)

An Application is trying to load localhost/res/123/var/abc and the content-type is set to:

application/json

The server should just return the JSON-Result to the application (which is implemented).

2)

If a normal web-browser is visiting the url localhost/res/123/var/abc, the content-type is set to:

text/html

In this case the server should redirect the browser to output a webview (e.g. index_web.html).

Can i realize something like this without using PHP?

like image 665
gies0r Avatar asked Jan 13 '23 11:01

gies0r


1 Answers

The content type is not set at request time, so far as I am aware. Thus, you would need to check if the browser is sending an Accept header, which describes to the server what kinds of data it will accept (and display) without downloading it. Most browsers do this. They send the following header string to the server (this is obtained from Chrome):

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Therefore, you would need to check the Accept header using a condition:

RewriteCond %{HTTP:Accept} text/html [NC]
# Then, add your RewriteRules here ...

This will check to see if text/html is contained in the Accept header.

like image 97
Mike Rockétt Avatar answered Jan 28 '23 01:01

Mike Rockétt