2010-09-09 28 views
51

我有一個網站叫移動設備重定向使用htaccess的

www.website.org

我有一個移動網站名爲

m.website.org

我想用一個htaccess到主網站的網址自動重定向到移動版..

但是,移動版本上有一個鏈接指向m艾因網站名爲

www.website.org?noredirect=true

當我點擊的實際網站其鏈接的主頁上的標識來

www.website.org

我不希望用戶被允許回到移動不小心通過點擊主頁上的徽標。我怎樣才能通過沒有JavaSCript的htaccess來實現這一點。

如果不是,我是開放的思想替代選項。

編輯

我想目前我會用這個通過htaccess的感應移動的重定向

RewriteEngine On 
RewriteBase/

RewriteCond %{HTTP_USER_AGENT} android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge\ |maemo|midp|mmp|opera\ m(ob|in)i|palm(\ os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows\ (ce|phone)|xda|xiino [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} ^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a\ wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r\ |s\)|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1\ u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp(\ i|ip)|hs\-c|ht(c(\-|\ |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac(\ |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt(\ |\/)|klon|kpt\ |kwc\-|kyo(c|k)|le(no|xi)|lg(\ g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-|\ |o|v)|zz)|mt(50|p1|v\)|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v\)|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-|\)|webc|whit|wi(g\ |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-) [NC] 
RewriteRule ^$ http://m.website.com [R,L] 
+0

你想怎麼確定的前期,他們應該被重定向到移動網站/你將如何堅持跨越請求noredirect狀態,如果他們點擊回到主網站? – 2010-09-10 07:36:00

+0

需要一些東西來檢查他們是否在手機上,或者我沒有設置JavaScript來改變CSS,但是如果他們試圖離開,它會重定向回去。 – balexander 2010-09-13 15:04:28

+1

您使用感應移動重定向的上次編輯存在一些錯誤。它有時會將W7上的IE8檢測爲移動設備。目前我無法再現這一點,但我會盡快進行更多調查。 – Marakoss 2012-07-16 13:06:12

回答

42

我測試位,以下的作品,但不是完整的規則設置其整體,所以如果你遇到麻煩,讓我知道,我會挖掘更多。但是,假設我得到的一切是正確的,你可以嘗試像以下:

RewriteEngine On 

# Check if this is the noredirect query string 
RewriteCond %{QUERY_STRING} (^|&)noredirect=true(&|$) 
# Set a cookie, and skip the next rule 
RewriteRule^- [CO=mredir:0:%{HTTP_HOST},S] 

# Check if this looks like a mobile device 
# (You could add another [OR] to the second one and add in what you 
# had to check, but I believe most mobile devices should send at 
# least one of these headers) 
RewriteCond %{HTTP:x-wap-profile} !^$ [OR] 
RewriteCond %{HTTP:Profile}  !^$ 
# Check if we're not already on the mobile site 
RewriteCond %{HTTP_HOST}   !^m\. 
# Check to make sure we haven't set the cookie before 
RewriteCond %{HTTP:Cookie}  !\smredir=0(;|$) 
# Now redirect to the mobile site 
RewriteRule^http://m.example.org%{REQUEST_URI} [R,L] 
+0

謝謝我會檢查出來! – balexander 2010-09-27 15:36:05

+0

任何判決? :)我喜歡Tim的代碼的外觀。 – Francisc 2011-06-04 20:41:24

+0

對不起,是的,它的工作! – balexander 2011-12-29 03:04:49

58

蒂姆·斯通的解決方案是在正確的軌道上,但他最初的重寫規則,並在最後的狀態他的cookie的名稱是不同的,你不能在相同的請求中寫入和讀取cookie。

這裏是完成工作代碼:

RewriteEngine on 
RewriteBase/
# Check if this is the noredirect query string 
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$) 
# Set a cookie, and skip the next rule 
RewriteRule^- [CO=mredir:0:www.website.com] 

# Check if this looks like a mobile device 
# (You could add another [OR] to the second one and add in what you 
# had to check, but I believe most mobile devices should send at 
# least one of these headers) 
RewriteCond %{HTTP:x-wap-profile} !^$ [OR] 
RewriteCond %{HTTP:Profile}  !^$ [OR] 
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC] 
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC] 

# Check if we're not already on the mobile site 
RewriteCond %{HTTP_HOST}   !^m\. 
# Can not read and write cookie in same request, must duplicate condition 
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) 

# Check to make sure we haven't set the cookie before 
RewriteCond %{HTTP_COOKIE}  !^.*mredir=0.*$ [NC] 

# Now redirect to the mobile site 
RewriteRule^http://m.website.com [R,L] 
+1

謝謝。一直在尋找重定向循環解決方案。迄今爲止,這個說得很好。 – swan 2011-02-13 16:56:37

+5

這是否將桌面版或移動版顯示爲ipad?如果是移動設備,默認情況下會如何顯示桌面版本?你有推薦更多地瞭解這些正則表達式嗎? – zylstra 2012-04-06 09:31:31

+1

這是一個很好的解決方案,但目前在移動Firefox/Chrome上不起作用,但內部Android瀏覽器正常工作。我猜想要添加更多的用戶代理。 可能我們需要的東西類似於:「Android |黑莓| ipad | iphone | ipod | iemobile | opera手機| palmos | webos |谷歌移動」 – 2015-09-09 09:17:56

20

我修改蒂姆·斯通的解決方案更進一步。這允許Cookie處於2個狀態,1個用於移動,0個用於全部。當移動cookie設置爲0時,即使移動瀏覽器也會轉到整個站點。

下面是代碼:

<IfModule mod_rewrite.c> 
    RewriteBase/
    RewriteEngine On 

    # Check if mobile=1 is set and set cookie 'mobile' equal to 1 
    RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$) 
    RewriteRule^- [CO=mobile:1:%{HTTP_HOST}] 

    # Check if mobile=0 is set and set cookie 'mobile' equal to 0 
    RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$) 
    RewriteRule^- [CO=mobile:0:%{HTTP_HOST}] 

    # cookie can't be set and read in the same request so check 
    RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$) 
    RewriteRule^- [S=1] 

    # Check if this looks like a mobile device 
    RewriteCond %{HTTP:x-wap-profile} !^$ [OR] 
    RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR] 
    RewriteCond %{HTTP:Profile}  !^$ 

    # Check if we're not already on the mobile site 
    RewriteCond %{HTTP_HOST}   !^m\. 
    # Check to make sure we haven't set the cookie before 
    RewriteCond %{HTTP:Cookie}  !\mobile=0(;|$) 
    # Now redirect to the mobile site 
    RewriteRule^http://m.example.com%{REQUEST_URI} [R,L] 
</IfModule> 
+0

用於添加'%{REQUEST_URI}'的+1 +1 – 2012-08-29 03:19:48

+0

比接受的答案好得多的代碼,它也更通用,可以更輕鬆地使用。謝謝! – Community 2013-09-18 16:55:12

+0

任何想法如何我可以避免重定向到移動網站的特定頁面?請參閱http://stackoverflow.com/questions/18871314/rewritecond-to-exclude-a-path-not-working謝謝! – Community 2013-09-18 17:47:20

-5

或者你可以試試這個:

​​
+2

這是OP所要求的一些嚴重的矯枉過正 – yfrancis 2012-11-04 21:37:59

+0

認真嗎?在「Hello World」中展示了所有技能......非常適合面試。 :) – 2015-06-06 08:37:44

-3

首先,到以下網址並下載mobile_detect。PHP文件:

http://code.google.com/p/php-mobile-detect/

插入您的索引或主頁的下面的代碼:

<?php 
@include("Mobile_Detect.php"); 
$detect = new Mobile_Detect(); 
if ($detect->isMobile() && isset($_COOKIE['mobile'])) 
{ 
$detect = "false"; 
} 
elseif ($detect->isMobile()) 
{ 
header("Location:http://www.yourmobiledirectory.com"); 
} 
?> 
-2

你也可以試試這個。積分原作者誰自刪除腳本

/mobile.class.php

<?php 
/* 
===================================================== 
Mobile version detection 
----------------------------------------------------- 
compliments of http://www.buchfelder.biz/ 
===================================================== 
*/ 

$mobile = "http://www.stepforth.mobi"; 
$text = $_SERVER['HTTP_USER_AGENT']; 
$var[0] = 'Mozilla/4.'; 
$var[1] = 'Mozilla/3.0'; 
$var[2] = 'AvantGo'; 
$var[3] = 'ProxiNet'; 
$var[4] = 'Danger hiptop 1.0'; 
$var[5] = 'DoCoMo/'; 
$var[6] = 'Google CHTML Proxy/'; 
$var[7] = 'UP.Browser/'; 
$var[8] = 'SEMC-Browser/'; 
$var[9] = 'J-PHONE/'; 
$var[10] = 'PDXGW/'; 
$var[11] = 'ASTEL/'; 
$var[12] = 'Mozilla/1.22'; 
$var[13] = 'Handspring'; 
$var[14] = 'Windows CE'; 
$var[15] = 'PPC'; 
$var[16] = 'Mozilla/2.0'; 
$var[17] = 'Blazer/'; 
$var[18] = 'Palm'; 
$var[19] = 'WebPro/'; 
$var[20] = 'EPOC32-WTL/'; 
$var[21] = 'Tungsten'; 
$var[22] = 'Netfront/'; 
$var[23] = 'Mobile Content Viewer/'; 
$var[24] = 'PDA'; 
$var[25] = 'MMP/2.0'; 
$var[26] = 'Embedix/'; 
$var[27] = 'Qtopia/'; 
$var[28] = 'Xiino/'; 
$var[29] = 'BlackBerry'; 
$var[30] = 'Gecko/20031007'; 
$var[31] = 'MOT-'; 
$var[32] = 'UP.Link/'; 
$var[33] = 'Smartphone'; 
$var[34] = 'portalmmm/'; 
$var[35] = 'Nokia'; 
$var[36] = 'Symbian'; 
$var[37] = 'AppleWebKit/413'; 
$var[38] = 'UPG1 UP/'; 
$var[39] = 'RegKing'; 
$var[40] = 'STNC-WTL/'; 
$var[41] = 'J2ME'; 
$var[42] = 'Opera Mini/'; 
$var[43] = 'SEC-'; 
$var[44] = 'ReqwirelessWeb/'; 
$var[45] = 'AU-MIC/'; 
$var[46] = 'Sharp'; 
$var[47] = 'SIE-'; 
$var[48] = 'SonyEricsson'; 
$var[49] = 'Elaine/'; 
$var[50] = 'SAMSUNG-'; 
$var[51] = 'Panasonic'; 
$var[52] = 'Siemens'; 
$var[53] = 'Sony'; 
$var[54] = 'Verizon'; 
$var[55] = 'Cingular'; 
$var[56] = 'Sprint'; 
$var[57] = 'AT&T;'; 
$var[58] = 'Nextel'; 
$var[59] = 'Pocket PC'; 
$var[60] = 'T-Mobile';  
$var[61] = 'Orange'; 
$var[62] = 'Casio'; 
$var[63] = 'HTC'; 
$var[64] = 'Motorola'; 
$var[65] = 'Samsung'; 
$var[66] = 'NEC'; 

$result = count($var); 

for ($i=0;$i<$result;$i++) 
{  
    $ausg = stristr($text, $var[$i]);  
    if(strlen($ausg)>0) 
    { 
     header("location: $mobile"); 
     exit; 
    } 

} 
?> 

只需編輯$mobile = "http://www.stepforth.mobi";

+4

這是一些嚴重不良的PHP代碼。 – Ariel 2013-03-12 07:33:23

+0

@Ariel你的理由? – TheBlackBenzKid 2013-03-12 11:01:59

+2

它是單獨編號的數組元素,這是愚蠢的。它使用66個單獨的命令創建數組,而不是一個數組初始化,這很慢。它使用for循環而不是foreach循環,這不是習慣用法。 – Ariel 2013-03-12 18:40:05

5

感謝蒂姆·斯通,naunu,和凱文·邦德,這些答案真的幫助了我。這是我對你的代碼的適應。如果用戶沒有通過移動設備訪問網站,我添加了從m.example.com重定向回桌面網站的功能。此外,我增加了一個環境變量來保存HTTP/HTTPS請求:

# Set an environment variable for http/https. 
RewriteCond %{HTTPS} =on 
RewriteRule ^(.*)$ - [env=ps:https] 
RewriteCond %{HTTPS} !=on 
RewriteRule ^(.*)$ - [env=ps:http] 

# Check if m=1 is set and set cookie 'm' equal to 1. 
RewriteCond %{QUERY_STRING} (^|&)m=1(&|$) 
RewriteRule^- [CO=m:1:example.com] 

# Check if m=0 is set and set cookie 'm' equal to 0. 
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$) 
RewriteRule^- [CO=m:0:example.com] 

# Cookie can't be set and read in the same request so check. 
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$) 
RewriteRule^- [S=1] 

# Check if this looks like a mobile device. 
RewriteCond %{HTTP:x-wap-profile} !^$ [OR] 
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR] 
RewriteCond %{HTTP:Profile} !^$ 
# Check if we're not already on the mobile site. 
RewriteCond %{HTTP_HOST} !^m\. 
# Check if cookie is not set to force desktop site. 
RewriteCond %{HTTP_COOKIE} !^.*m=0.*$ [NC] 
# Now redirect to the mobile site preserving http or https. 
RewriteRule^%{ENV:ps}://m.example.com%{REQUEST_URI} [R,L] 

# Check if this looks like a desktop device. 
RewriteCond %{HTTP_USER_AGENT} "!(android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile)" [NC] 
# Check if we're on the mobile site. 
RewriteCond %{HTTP_HOST} ^m\. 
# Check if cookie is not set to force mobile site. 
RewriteCond %{HTTP_COOKIE} !^.*m=1.*$ [NC] 
# Now redirect to the mobile site preserving http or https. 
RewriteRule^%{ENV:ps}://example.com%{REQUEST_URI} [R,L] 

這似乎只是一兩件事,做工精細:當我在桌面版網站與桌面設備和我訪問m.example.com/ ?m = 1,我被重定向到example.com。當我再次嘗試時,我「留在」m.example.com。看起來好像cookie第一次沒有被正確設置和/或正確讀取。

也許有更好的方法來確定設備是否是桌面設備,我只是否定了上面的設備檢測。

我想知道是否這樣檢測到所有移動設備。在Tim Stone和naunu的代碼中,部分要大得多。

2

同樣,如果你想重定向到一個子文件夾,而不是一個子域,請執行下列操作:

工作過凱文的偉大的解決方案,你可以在你的站點的根目錄將其添加到.htaccess文件目錄:

<IfModule mod_rewrite.c> 
     RewriteBase/
     RewriteEngine On 

     # Check if mobile=1 is set and set cookie 'mobile' equal to 1 
     RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$) 
     RewriteRule^- [CO=mobile:1:%{HTTP_HOST}] 

     # Check if mobile=0 is set and set cookie 'mobile' equal to 0 
     RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$) 
     RewriteRule^- [CO=mobile:0:%{HTTP_HOST}] 

     # cookie can't be set and read in the same request so check 
     RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$) 
     RewriteRule^- [S=1] 

     # Check if this looks like a mobile device 
     RewriteCond %{HTTP:x-wap-profile} !^$ [OR] 
     RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR] 
     RewriteCond %{HTTP:Profile}  !^$ 

     # Check if we're not already on the mobile site 
     RewriteCond %{HTTP_HOST}   !^m\. 
     # Check to make sure we haven't set the cookie before 
     RewriteCond %{HTTP:Cookie}  !\mobile=0(;|$) 
     # Now redirect to the mobile site 
     RewriteRule^http://www.mysite.com/m/ [R] 
    </IfModule> 

然後,在/m/文件夾中添加或創建具有以下一個.htaccess:

#Begin user agent loop fix 

     RewriteEngine Off 
     RewriteBase/

    #End user agent loop fix 

我知道它不是一個直接的ANS想問這個問題,但有人(像我)可能會偶然發現這個問題,並想知道如何實現該方法。

+0

,但這是個好主意。謝謝 – dhpratik 2014-11-03 09:26:55

-1

對於手機像domain.com/m/

RewriteCond %{HTTP_REFERER} !^http://(.*).domain.com/.*$ [NC] 
RewriteCond %{REQUEST_URI} !^/m/.*$ 
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC] 
RewriteRule ^(.*)$ /m/ [L,R=302]