简体   繁体   English

如何将移动网站用户重定向到移动版式

[英]How to redirect mobile site users to a mobile layout

I can't find some solid information on what I'm specifically looking for, nor do I know the best method of doing this (in code or using HTACCESS); 我找不到我想要的确切信息,也不知道执行此操作的最佳方法(使用代码或使用HTACCESS); to redirect mobile users to http://m.example.com 将移动用户重定向到http://m.example.com

I am looking to enable both mobile, and PC users to view our website in both devices, with a user-friendly layout to accommodate for both users. 我希望使移动用户和PC用户都能在这两种设备中查看我们的网站,并采用用户友好的布局来容纳这两个用户。

The solution without HTACCESS: 没有HTACCESS的解决方案:

Best way is store the info about which layout is selected in Cookie. 最好的方法是存储有关在Cookie中选择哪种布局的信息。

if(isset($_GET["setMobile"])) {
  setcookie("m", $_GET["setMobile"], time()+(60*60*24*31), "/");
}

And then 接着

if($_COOKIE["m"]) {
  /* MOBILE LAYOUT */
}

I thnk you do not even Need htaccess. 我想你甚至不需要htaccess。 You can create index.php in your subfolder m.domain.com and place there Cookie Definition. 您可以在子文件夹m.domain.com创建index.php并将其放置在Cookie定义中。

setcookie("m", true, time()+(60*60*24*31), "/");

Solution: A : To redirect mobile users to mobile layout 解决方案:A:将移动用户重定向到移动布局

Via .htaccess 通过.htaccess

RewriteEngine On
RewriteCond %{QUERY_STRING} !^desktop
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|iphone|ipod|#opera mobile|palmos|webos" [NC]
RewriteRule ^$ http://m.example.com [L,R=302]

===================== =====================

Solution: B: Redirect mobile site users to mobile layout and from mobile to desktop 解决方案:B:将移动网站用户重定向到移动布局,以及从移动设备重定向到桌面

Via PHP Session 通过PHP会话

<a href="<?php echo $_SERVER['PHP_SELF'], '?desktop=1' ?>">Go to Desktop version</a>

<?php
if(!isset($_SESSION['desktop'])) {
    $_SESSION['desktop'] = false;
}

if(isset($_GET['desktop']) && $_GET['desktop'] == 1) {
    $_SESSION['desktop'] = true;
}

if(!$_SESSION['desktop']) {
    if ( !$detect->isMobile() ) {
      header('Location: http://m.example.com/');
    }
}
?>

On your mobile site, you can pass a get parameter of desktop=1. 在您的移动网站上,您可以传递get参数desktop = 1。 Normally, if the parameter is not passed, and if the session is not set, it's false. 通常,如果未传递参数,并且未设置会话,则为false。 On false value of $_SESSION['desktop'], you continue your script so it redirects to mobile. 在$ _SESSION ['desktop']的错误值上,您继续执行脚本,以便将其重定向到移动设备。 But once the param is passed, it changes the session to true, and you block will not be executed, so the normal (desktop) content of the site will be visible. 但是一旦参数通过,它将会话更改为true,并且您的阻止将不会执行,因此该站点的常规(桌面)内容将可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM