Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

3 Answers

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))) {
   location.replace("http://url-to-send-them/iphone.html");
}
-->
</script>

There are loads of site to assist you in this - http://www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript/

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 - stackoverflow.com/questions/1005153/… –  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
add comment

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

<?php
$THEPAGE = 'WHATEVER THE URL YOU WANT IT TO BE';
$page = file_get_contents("$THEPAGE");
if(strstr($page, '404'))
{
}
else
{
header('Location: ' .$THEPAGE .'\')';
}
?>
share|improve this answer
    
Needs to be apache –  ckliborn May 30 '12 at 18:35
add comment

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
add comment

Your Answer

 
discard

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.