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 am using Apache's mod_rewrite to redirect mobile users to my mobile site based on their http_user_agent. However not all pages have a mobile equivalent. Also mobile pages end in .html and "full" pages end in .shtml.

Here is some pseudo code.

Does the user have a certain HTTP_USER_AGENT?

Is there a mobile page?

If so take them there. If not, no redirection is needed.

share|improve this question

RewriteCond allows backreferences[0] to capture groups in the RewriteRule.

RewriteCond %{HTTP_USER_AGENT} Mobile
RewriteCond $1.html -f
RewriteRule ^(.*)\.shtml$ $1.html [R]

[0] More like a forward reference actually...

share|improve this answer

Probably best post this in webmasters section. But you want something like the following i presume?

<script language=javascript>

if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {

There are loads of site to assist you in this -

Just simply add the script to the pages you want detection on, there is no need to pre-check for a site page existence.

share|improve this answer
Ok I can try there - I want to do this with apache and not javascript – ckliborn May 30 '12 at 16:03
This is what you need in that case -… – Cold T May 30 '12 at 16:27
That helps but it does not test for the existence of a file before redirecting there. – ckliborn May 30 '12 at 16:49

Is there any way you can use php to do this, then you could do;

$page = file_get_contents("$THEPAGE");
if(strstr($page, '404'))
header('Location: ' .$THEPAGE .'\')';
share|improve this answer
Needs to be apache – ckliborn May 30 '12 at 18:35

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.