Jump to content

Setting content type response header based on accept request headers


ghell

Recommended Posts

Apologies if this has already been asked, I couldn't find it.

 

Is there any way of using apache (.ht... rather than the server config files) to send a content type based on the accept headers and the file extension? For example XHTML being sent with the correct mime type only to browsers that say they can handle it and using text/html otherwise.

Link to comment
Share on other sites

Thanks for the reply but I don't think I can do it with BrowserMatch.

 

A specific example of what I'm trying to do is something like

If "accept" contains "application/xhtml+xml" and the resource name being requested ends in ".xhtml" then set "content-type" to "application/xhtml+xml"

 

I may be able to do the accept check and content-type set in BrowserMatch (not completely sure) but even if I can, I wouldn't know how to do it just for the .xhtml files in the directory (and preferably subdirectories).

 

This sort of thing normally seems to be done in a server side language but I'm not using one for a particular set of xhtml 1.1 pages and I don't really want to use one just for the sake of it, if apache .ht* files can do it instead.

 

I would have thought it would be fairly common to do this to distribute obscure files with the correct file type, for example if an x3d file with the "model/x3d+xml" mime.

Link to comment
Share on other sites

Tricky in config since all you have is a static ruleset plus mod_rewrite - no IF..THEN..ELSE

 

However a sneaky trick is to use mod_rewrite to set an ENV variable then apply a directive conditional on that ENV

 

Set your ENV dependent on the filetype using a rewrite rule then use whatever other static directives you need but made conditional on that ENV var.

 

This is the way i've gotten round similar requiremens on my own server such as the need to flag conditional logging for selected filetypes etc.

 

See the examples and directives on this page including the e.g.s at the foot of the page

http://httpd.apache.org/docs/2.0/env.html

Link to comment
Share on other sites

I tried playing with

SetEnvIfNoCase Accept "application/xhtml\+xml" is_xhtml=true

but no additional environment variables appeared (I was only testing with phpinfo(), I assume that that enumerates them rather than just displaying a fixed set) Where am I going wrong so far?

Link to comment
Share on other sites

Never tried checking the O/S environment. I may be wrong but I'm fairly sure that the ENV will be local to Apache.

This may differ between Linii and Win32 O/Ss though - I use Win32 where the current proc generally cannot alter the parent ENV

(there were ways in Win9x days by altering via the program segment prefix - PSP - but that's a bit different in NTx variants).

 

Also there is no logical true/false concept as with a programming language the ENV is either set with value or not. This may be confusing if you need to use mod_rewrite to test pesudo-logical values where you should find the "logical" equivalents are numeric 1/0 with an omitted rvalue assuming true since the var is "set" but with no textual value needed

 

Example: http://httpd.apache.org/docs/2.0/mod/mod_setenvif.html#setenvifnocase

SetEnvIfNoCase Host Apache\.Org site=apache

 

I use mod_geoip so heres an example - set logical true (note omitted Rvalue)

    SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry

#...

    Deny from env=BlockCountry

 

Here's an example I use handling multiple "logical" ENVs to set another ENV which may help you build up the logic - I found that the actual logic etc. isn't very well explained anywhere and it took some research to be able to do what you see below. All are set with no rvalue.

#Limit logging...
RewriteCond    %{ENV:locallog} =1 [OR]
RewriteCond    %{ENV:css-request} =1 [OR]
RewriteCond    %{ENV:image-request} =1 [OR]
RewriteCond    %{ENV:js-request} =1 
RewriteRule    .*  -  [Last,Env=dont-log:1]

Link to comment
Share on other sites

Ok, thanks. I have not had much experience with Apache, I generally use IIS for work. One of the download providers we use uses apache though. In IIS I have set environment variables and been able to enumerate them in scripts, so I assumed that it was also the case here. I just wanted to see some kind of output so that I could check what I was doing really.

 

I was just using "true" as a string so that I could see what was going on, rather than assuming there were boolean values.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.