Redirection for Mobile using .htaccess - only on homepage

故事扮演 提交于 2019-12-13 02:26:38

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/12807250/redirection-for-mobile-using-htaccess-only-on-homepage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!