[英]Redirect a site to a Mobile version, error with Google Mobile-Friendly Test
我有以下網址:
domain.fr (desktop site)
domain.fr/m/ (mobile site)
我想將移動設備上的用戶重定向到:domain.fr/m/
在PHP中,我試過:
$useragent=$_SERVER['HTTP_USER_AGENT'];
if(preg_match('/(android|bb\d+).+mobile|....',substr($useragent,0,4))){header('Location: http://domain.fr/m/');}
問題:當我使用Google移動友好測試檢查domain.fr時,收到以下錯誤消息:
(就像谷歌無法檢查這是否適合移動設備)
如果我刪除上面的PHP,谷歌可以進行測試但是說domain.fr不是用戶友好的。
如何重定向到移動網站,我認為這是PHP代碼的問題,任何想法?
我認為您的代碼中存在錯誤,請嘗試分析Web服務器的日志。
我在谷歌移動友好測試和谷歌頁面速度測試,一切正常。
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:
$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 );
}
}
但我建議使用nginx進行此類重定向
@Julien首先,不要關閉Link標簽!
<link rel="alternate" href="http://luckeo.fr/m/" media="only screen and (max-width: 640px)">
替代“鏈接”它只是SEO鏈接,它不會重定向任何用戶。 而且,您必須將Google和其他用戶重定向到移動版。 閱讀這篇文章: https : //developers.google.com/webmasters/mobile-sites/mobile-seo/common-mistakes/faulty-redirects?hl = fr
你從谷歌測試得到“dismis”,因為你的php重定向有一些錯誤,請嘗試下面的重定向
好吧,我找到了解決方案:
php重定向是正確的,但代碼是針對移動和桌面站點執行的。
我不得不僅為domain.fr進行重定向,否則重定向無效(在移動網站上也執行),因此谷歌發送此錯誤消息“解雇”..這不容易理解..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.