2017-06-29 66 views
1

我想拿出一個htaccess的代碼,將允許我重定向用戶在所有移動設備去網頁開始www.example.com/blogexample.com/blog到特定頁面 - 幾乎與以下擴展名相同的鏈接:/mobile添加'/移動'到移動設備上的網址

使移動設備的網站成爲:

www.example.com/blog -> www.example.com/blog/mobile 

example.com/blog -> example.com/blog/mobile 

我怎麼能這樣做?

回答

1

你可以把這個規則在站點根的.htaccess:

RewriteEngine On 

RewriteCond %{HTTP:x-wap-profile} !^$ [OR] 
RewriteCond %{HTTP:Profile}  !^$ 
RewriteRule ^blog/?$ /$0/mobile [L,NC,R=302] 

這是假設你沒有.htaccessblog/目錄。

如果你已經有了一個blog/.htaccess然後使用這個規則blog/.htaccess

RewriteEngine On 

RewriteCond %{HTTP:x-wap-profile} !^$ [OR] 
RewriteCond %{HTTP:Profile}  !^$ 
RewriteRule ^/?$ /blog/mobile [L,R=302] 

如果上面標頭不工作,然後用user agent基於檢查:

RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile|windows ce|epoc|opera" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox"[NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "blackberry|ericsson,|panasonic|philips|sanyo|sharp|sie-"[NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|danger|palm|series60|palmsource|pocketpc"[NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,|alcatel|ericy|vodafone\/|wap1\.|wap2\.|iPhone|android"[NC] 
RewriteRule ^blog/?$ /$0/mobile [L,NC,R=302] 
+0

如何可靠的是'用於檢測「移動設備」的「x-wap-profile」或「Profile」標題?我的移動設備都沒有發送任何這些頭文件? – MrWhite

+1

好吧然後第二個選項是使用'HTTP_USER_AGENT'條件在我編輯的答案。 – anubhava