Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a set of RewriteCond rules that test for various mobile devices and then set environment variables like "env=device:.iphone" or "env=device:.smartphone" if the useragent matches an iPhone or Android device.

I'm trying to now redirect the user to custom-styled 404/500 server error pages for each device, by way of the error pages.

Ideally I'd like to be able to test for a variable being there, and then write in a custom ErrorDocument string. But an apache doesn't seem to work in this case.

Any ideas how I can construct if/else tests in an apache conf file for environment vars?

share|improve this question

Starting with version 2.3 of Apache, you can make use of IF constucts. It's described in Apache's documentation.

share|improve this answer
Does this change the fact above that ErrorDocument currently (or at least on 2.2) doesn't allow for conditional processing? – Tad Apr 19 '12 at 14:38
Indeed a shame that it's not possible in 2.2, but it's worth the upgrade. Also, no mention of 2.2 as specific requirement. – sonic May 16 '12 at 11:54

ErrorDocument doesn't allow for conditional anything, but it does do an internal redirect.

You should be able to accomplish this with mod_rewrite:

ErrorDocument 404 /normal_404.html

RewriteEngine on
RewriteCond %{ENV:device:.iphone} 1
RewriteRule ^/normal_404.html$ /iphone_404.html [L]
share|improve this answer
thanks a bunch for this. However, it doesn't look like the rewrite rule has an effect on the errordocument. Tried taking the condition off as well, to make sure, and no rewrite rules seem to apply to it. Perhaps the rewrite rule needs to go into effect before the ErrorDocument is mapped? – Tad Sep 22 '11 at 19:50
The psudocode (psudoconfig) I was trying to do was something like: <if environment variable = iphone> ErrorDocument 404 /error_pages/404.iphone.html </if> I just haven't found a way in apache conf files to test for an environment variable - just to set it. – Tad Sep 22 '11 at 19:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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