简体   繁体   English

将网站重定向到移动版,Google移动友好测试时出错

[英]Redirect a site to a Mobile version, error with Google Mobile-Friendly Test

I have the following urls : 我有以下网址:

domain.fr     (desktop site)
domain.fr/m/  (mobile site)
  • Both urls "point" to each other using"canonical" or "alternate". 两个网址都使用“规范”或“替代”相互“指向”。
  • We can access those urls without problem. 我们可以毫无问题地访问这些网址。

I would like to redirect people on mobile to : domain.fr/m/ 我想将移动设备上的用户重定向到:domain.fr/m/

in PHP, I tried : 在PHP中,我试过:

$useragent=$_SERVER['HTTP_USER_AGENT'];

if(preg_match('/(android|bb\d+).+mobile|....',substr($useragent,0,4))){header('Location: http://domain.fr/m/');}

Problem : When I check domain.fr with Google Mobile-Friendly Test, I get this error message : 问题:当我使用Google移动友好测试检查domain.fr时,收到以下错误消息:

在此输入图像描述

(it's like Google can't check if this is mobile friendly) (就像谷歌无法检查这是否适合移动设备)

If I remove the PHP above, Google can do the test but says domain.fr is NOT user-friendly. 如果我删除上面的PHP,谷歌可以进行测试但是说domain.fr不是用户友好的。

How to make a redirection to the mobile site, I think it's a problem with the PHP code, any idea ? 如何重定向到移动网站,我认为这是PHP代码的问题,任何想法?

I think you have a bug in your code, try to analyze logs of your web server. 我认为您的代码中存在错误,请尝试分析Web服务器的日志。

I have test at Google Mobile-friendly test and in Google Page Speed, all works fine. 我在谷歌移动友好测试和谷歌页面速度测试,一切正常。

Nginx logs: Nginx日志:

127.0.0.1 - - [22/Aug/2015:16:29:16 +0300] "GET /test.mobile.php HTTP/1.1" 301 18 "-" "Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12F70 Safari/600.1.4 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
127.0.0.1 - - [22/Aug/2015:16:29:18 +0300] "GET / HTTP/1.1" 200 17211 "-" "Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12F70 Safari/600.1.4 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"

PHP: PHP:

$useragent = $_SERVER['HTTP_USER_AGENT'];

$devices = ['iphone', 'android'];
if ( arrayInString( $devices, strtolower( $useragent ) ) ) {
    header("HTTP/1.0 301 Moved Permanently");
    header("Location: http://mysite.ua" . strtolower( $_SERVER['REQUEST_URI'] ) );
    die("Redirect");
}


function arrayInString( $inArray , $inString ) {
    if( is_array( $inArray ) ) {
        foreach( $inArray as $e ) {
            if( strpos( $inString , $e ) !== false )
                return true;
        }
        return false;
    } else {
        return ( strpos( $inString , $inArray ) !== false );
    }
}

But I recomend use nginx for such redirection 但我建议使用nginx进行此类重定向

@Julien First, do not close Link tag! @Julien首先,不要关闭Link标签!

<link rel="alternate" href="http://luckeo.fr/m/" media="only screen and (max-width: 640px)">

Alternate "link" its just SEO link, it does not redirect any user. 替代“链接”它只是SEO链接,它不会重定向任何用户。 And, you must redirect google and other users to mobile version. 而且,您必须将Google和其他用户重定向到移动版。 Read this article: https://developers.google.com/webmasters/mobile-sites/mobile-seo/common-mistakes/faulty-redirects?hl=fr 阅读这篇文章: https//developers.google.com/webmasters/mobile-sites/mobile-seo/common-mistakes/faulty-redirects?hl = fr

You got "dismis" from google tests, because you have some bugs in your php-redirects, try my redirect below 你从谷歌测试得到“dismis”,因为你的php重定向有一些错误,请尝试下面的重定向

okay I found the solution : 好吧,我找到了解决方案:

The php redirection was correct, but the code was executed for both mobile and desktop sites. php重定向是正确的,但代码是针对移动和桌面站点执行的。

I had to make the redirection only for domain.fr, otherwise the redirection was infinited (executed also when on mobile site), and consequently Google sent this error message "dismiss".. it was not easy to understand.. 我不得不仅为domain.fr进行重定向,否则重定向无效(在移动网站上也执行),因此谷歌发送此错误消息“解雇”..这不容易理解..

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

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