0

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.

ckliborn
  • 2,750
  • 4
  • 24
  • 36

3 Answers3

1

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...

mgorven
  • 30,036
  • 7
  • 76
  • 121
0

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.

Cold T
  • 2,391
  • 2
  • 16
  • 28
0

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 .'\')';
}
?>
e__
  • 61
  • 1
  • 7