1

I started by using the method described here in order to create a Mobile redirect, and it works perfectly.

What I need to do next, however, is prevent it from happening on any page other than the homepage. In other words: If the user loads the homepage from a mobile device, the redirect should happen - but if they load any other page from a mobile device, the redirect should not occur.

I'd love any advice the community might be able to provide as to how to accomplish this effectively.

Community
  • 1
  • 1
hdwebpros
  • 528
  • 3
  • 7
  • 20

2 Answers2

0

You need to redirect by checking out the User-Agent:

Mobile website redirection based on user agent

or on PHP:

<?php

$useragent = $_SERVER['HTTP_USER_AGENT'];

//mobile example
if( strpos($useragent,"Blackberry") ) { 
header("Location: http://m.nickyeoman.com/");
}

//css example
if( strpos($useragent,"wii") ) { ?>
<link rel="stylesheet" href="/css/wii.css" type="text/css" media="all" />
<php } else { ?>
<link rel="stylesheet" href="/css/global.css" type="text/css" media="all" />
<php } ?>

by http://www.nickyeoman.com/blog/php/64-php-redirect

Community
  • 1
  • 1
Eun
  • 4,146
  • 5
  • 30
  • 51
  • That part works fine. I'm already doing that. That's not what I'm inquiring about. I used .htaccess for that and it works great. – hdwebpros Oct 09 '12 at 19:56
  • What I'm trying to do is this... if they are trying to access that is not the homepage, then they do NOT get redirected. So, if they ARE opening the homepage, the redirect works as normal. If, for example, they are opening anything else such as the blog, it does not redirect to the mobile site. – hdwebpros Oct 12 '12 at 17:19
  • How about an statement or directive? Is that possible? – hdwebpros Oct 13 '12 at 19:58
0

I just had to add

[OR]
RewriteCond %{HTTP_HOST}  ^(mydomain\.com|www\.mydomain\.com)$ [NC]

That was it. So, in the end, it looked like

# Check if we're not already on the mobile site AND just going to the homepage
RewriteCond %{HTTP_HOST}    !^m\. [OR]
RewriteCond %{HTTP_HOST}    ^(mydomain\.com|www\.mydomain\.com)$ [NC]
# Can not read and write cookie in same request, must duplicate condition

Make sure you get the [or] in there too. Hopefully my newb advice will help someone someday

hdwebpros
  • 528
  • 3
  • 7
  • 20