2

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?

Tad
  • 133

3 Answers3

3

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

chutz
  • 8,300
sonic
  • 31
1

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]
Shane Madden
  • 116,404
  • 13
  • 187
  • 256
0

Other answers say that you should be able to use Apache 2.3's <If> condition, but don't give an example or explain the syntax. Here is how you would check an environment variable and then set a custom error document based on it in .htaccess:

<If "reqenv('device') == 'iphone'">
     ErrorDocument 404 /error_pages/404.iphone.html
</If>