【问题标题】:Redirection for Mobile using .htaccess - only on homepage使用 .htaccess 为移动设备重定向 - 仅在主页上
【发布时间】:2012-09-30 05:03:03
【问题描述】:

我首先使用here 描述的方法来创建移动重定向,并且效果很好。

然而,接下来我需要做的是防止它发生在除主页之外的任何页面其他上。换句话说:如果用户从移动设备加载主页,则应该发生重定向 - 但如果他们从移动设备加载任何其他页面,则不应发生重定向。

我希望社区能够就如何有效地实现这一目标提供任何建议。

【问题讨论】:

    标签: .htaccess redirect mobile wildcard http-host


    【解决方案1】:

    您需要通过签出 User-Agent 来重定向:

    Mobile website redirection based on user agent

    或在 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 } ?>
    

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

    【讨论】:

    • 那部分工作正常。我已经在这样做了。这不是我要询问的。我为此使用了 .htaccess,效果很好。
    • 我想要做的是......如果他们试图访问不是主页,那么他们不会被重定向。因此,如果他们正在打开主页,则重定向会正常工作。例如,如果他们打开博客等其他内容,则不会重定向到移动网站。
    • 语句或指令怎么样?这可能吗?
    【解决方案2】:

    我只需要添加

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

    就是这样。所以,最后,它看起来像

    # 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
    

    确保您也将 [or] 放入其中。希望我的新手建议有一天能对某人有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多