2010-09-09 26 views
51

Tôi có một trang web được gọiMobile Redirect bằng cách sử dụng htaccess

www.website.org

Tôi có một trang web di động gọi

m.website.org

Tôi muốn sử dụng một htaccess để tự động chuyển hướng URL của trang web chính để phiên bản dành cho điện thoại di động ..

Tuy nhiên, có một liên kết trên phiên bản di động trỏ ngược lại m website ain gọi

www.website.org?noredirect=true

Khi tôi nhấp vào biểu tượng trên trang chủ của trang web thực tế nó liên kết đến

www.website.org

Tôi không muốn người dùng được phép trở lại điện thoại di động vô tình bằng cách nhấp vào biểu trưng trên trang chính. Làm thế nào tôi có thể thực hiện điều này thông qua htaccess mà không cần JavaSCript.

Nếu tôi không cởi mở với các tùy chọn thay thế.

EDIT

Tôi nghĩ Tôi hiện đang đi sử dụng này cho cảm chuyển hướng di động thông qua 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

Làm thế nào để bạn muốn xác định trả trước rằng họ nên được chuyển hướng đến trang web di động/làm thế nào bạn sẽ kéo dài tình trạng noredirect qua yêu cầu nếu họ nhấp lại vào trang web chính? –

+0

Có cần phải có một cái gì đó để kiểm tra xem họ đang ở trên một chiếc điện thoại di động hay không mà tôi đã thiết lập một số JavaScript để thay đổi CSS nhưng nếu họ cố gắng để nó sẽ chuyển hướng trở lại. – balexander

+1

EDIT cuối cùng của bạn mà bạn sử dụng để cảm biến chuyển hướng trên thiết bị di động có một số lỗi trong đó. Đôi khi nó phát hiện IE8 trên W7 là thiết bị di động. Tôi không thể tái tạo điều này vào lúc này, nhưng tôi sẽ giữ cho bạn được đăng ngay khi tôi điều tra thêm. – Marakoss

Trả lời

42

Tôi đã thử nghiệm bit và miếng sau đây, nhưng không phải là quy tắc hoàn toàn đặt trong nó toàn bộ, vì vậy nếu bạn gặp rắc rối với nó, hãy cho tôi biết và tôi sẽ đào sâu hơn một chút. Tuy nhiên, giả sử tôi có mọi thứ chính xác, bạn có thể thử một số thông tin như sau:

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

Cảm ơn tôi sẽ kiểm tra! – balexander

+0

Mọi bản án? :) Tôi thích giao diện của mã Tim. – Francisc

+0

Xin lỗi, vâng nó đã hoạt động! – balexander

58

Giải pháp của Tim Stone đang đi đúng hướng, nhưng tên viết tắt ban đầu và tên cookie trong điều kiện cuối cùng là khác nhau và bạn không thể viết và đọc cookie trong cùng một yêu cầu.

Đây là mã làm việc hoàn tất:

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

Cảm ơn. Đã tìm kiếm giải pháp vòng lặp chuyển hướng. Điều này một địa chỉ nó cho đến nay. – swan

+5

Điều này có hiển thị phiên bản dành cho máy tính để bàn hoặc thiết bị di động cho iPad không? Nếu điện thoại di động, làm thế nào sẽ đi về hiển thị các phiên bản máy tính để bàn theo mặc định? Bạn có đề xuất để tìm hiểu thêm về các regex này không? – zylstra

+1

Đây là giải pháp tốt nhưng hiện không hoạt động trên Firefox/Chrome trên thiết bị di động nhưng trình duyệt Android nội bộ hoạt động tốt. Tôi đoán thêm nhiều tác nhân người dùng. Có lẽ chúng ta cần một cái gì đó như: "android | blackberry | ipad | iphone | ipod | iemobile | opera di động | palmos | webos | googlebot-mobile" –

20

tôi sửa đổi giải pháp Tim Stone hơn nữa. Điều này cho phép cookie ở trong 2 trạng thái, 1 cho điện thoại di động và 0 cho đầy đủ. Khi cookie trên thiết bị di động được đặt thành 0 ngay cả khi trình duyệt trên điện thoại di động sẽ chuyển đến trang web đầy đủ.

Đây là mã:

<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

+1 để thêm '% {REQUEST_URI}' –

+0

Mã tốt hơn nhiều so với câu trả lời được chấp nhận, nó cũng chung chung hơn cho phép sử dụng dễ dàng hơn. Cảm ơn! – Community

+0

Bất kỳ ý tưởng nào về cách tôi có thể tránh chuyển hướng một trang cụ thể đến trang web dành cho thiết bị di động? Vui lòng xem http://stackoverflow.com/questions/18871314/rewritecond-to-exclude-a-path-not-working Cảm ơn! – Community

-5

Hoặc bạn có thể thử này:

?php 

/** 
* Mobile Detect 
* @license http://www.opensource.org/licenses/mit-license.php The MIT License 
*/ 
class Mobile_Detect 
{ 
    protected $accept; 
    protected $userAgent; 
    protected $isMobile = false; 
    protected $isAndroid = null; 
    protected $isAndroidtablet = null; 
    protected $isIphone = null; 
    protected $isIpad = null; 
    protected $isBlackberry = null; 
    protected $isBlackberrytablet = null; 
    protected $isOpera = null; 
    protected $isPalm = null; 
    protected $isWindows = null; 
    protected $isWindowsphone = null; 
    protected $isGeneric = null; 
    protected $devices = array(
    "android" => "android.*mobile", 
    "androidtablet" => "android(?!.*mobile)", 
    "blackberry" => "blackberry", 
    "blackberrytablet" => "rim tablet os", 
    "iphone" => "(iphone|ipod)", 
    "ipad" => "(ipad)", 
    "palm" => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)", 
    "windows" => "windows ce; (iemobile|ppc|smartphone)", 
    "windowsphone" => "windows phone os", 
    "generic" => "(kindle|mobile|mmp|midp|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap|opera mini)"); 

    public function __construct() 
    { 
     $this->userAgent = $_SERVER['HTTP_USER_AGENT']; 
     $this->accept = $_SERVER['HTTP_ACCEPT']; 

     if (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) 
     { 
      $this->isMobile = true; 
     } 
     elseif (strpos($this->accept, 'text/vnd.wap.wml') > 0 || strpos($this->accept, 'application/vnd.wap.xhtml+xml') > 0) 
     { 
      $this->isMobile = true; 
     } 
     else 
     { 
      foreach ($this->devices as $device => $regexp) 
      { 
       if ($this->isDevice($device)) 
       { 
        $this->isMobile = true; 
       } 
      } 
     } 
    } 

    /** 
    * Overloads isAndroid() | isAndroidtablet() | isIphone() | isIpad() | isBlackberry() | isBlackberrytablet() | isPalm() | isWindowsphone() | isWindows() | isGeneric() through isDevice() 
    * 
    * @param string $name 
    * @param array $arguments 
    * @return bool 
    */ 
    public function __call($name, $arguments) 
    { 
     $device = substr($name, 2); 
     if ($name == "is" . ucfirst($device) && array_key_exists(strtolower($device), $this->devices)) 
     { 
      return $this->isDevice($device); 
     } 
     else 
     { 
      trigger_error("Method $name not defined", E_USER_WARNING); 
     } 
    } 

    /** 
    * Returns true if any type of mobile device detected, including special ones 
    * @return bool 
    */ 
    public function isMobile() 
    { 
     return $this->isMobile; 
    } 

    protected function isDevice($device) 
    { 
     $var = "is" . ucfirst($device); 
     $return = $this->$var === null ? (bool) preg_match("/" . $this->devices[strtolower($device)] . "/i", $this->userAgent) : $this->$var; 
     if ($device != 'generic' && $return == true) { 
     $this->isGeneric = false; 
    } 
    return $return; 
} 
+2

đây là một số vấn đề nghiêm trọng đối với những gì OP đã yêu cầu – yfrancis

+0

Nghiêm túc? Thể hiện tất cả các kỹ năng chỉ trong một "Hello World" ... Tuyệt vời cho một cuộc phỏng vấn. :) –

-3

Trước tiên, hãy vào URL sau và tải về mobile_detect.php file:

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

Chèn đoạn mã sau vào trang chỉ mục hoặc nhà của bạn:

<?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

Bạn cũng có thể thử loại này. Tín dụng cho các tác giả gốc từ người đã loại bỏ kịch bản

/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; 
    } 

} 
?> 

Chỉ cần chỉnh sửa $mobile = "http://www.stepforth.mobi";

+4

Đó là một số mã PHP nghiêm trọng nghiêm trọng. – Ariel

+0

@Ariel lý do của bạn? – TheBlackBenzKid

+2

Nó đánh số riêng các phần tử mảng, điều này thật ngớ ngẩn. Nó tạo ra các mảng trong 66 lệnh riêng biệt thay vì một khởi tạo mảng, mà là chậm. Nó sử dụng một vòng lặp for cho mảng thay vì một vòng lặp foreach, mà không phải là thành ngữ. – Ariel

5

Cảm ơn Tim Stone, naunu, và Kevin Bond, những câu trả lời thực sự đã giúp tôi. Đây là sự thích ứng của tôi về mã của bạn. Tôi đã thêm chức năng được chuyển hướng trở lại trang web dành cho máy tính để bàn từ m.example.com trong trường hợp người dùng không truy cập trang web bằng thiết bị di động. Ngoài ra tôi đã thêm một biến môi trường để bảo vệ yêu cầu 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] 

Điều này dường như làm việc tốt, ngoại trừ một điều: Khi tôi đang trên trang web máy tính để bàn với một thiết bị máy tính để bàn và tôi ghé thăm m.example.com/ ? m = 1, tôi được chuyển hướng đến example.com. Khi tôi thử lại, tôi "ở lại" tại m.example.com. Dường như cookie không được đặt và/hoặc đọc chính xác lần đầu tiên.

Có thể có cách tốt hơn để xác định xem thiết bị có phải là thiết bị máy tính để bàn không, tôi vừa phủ nhận việc phát hiện thiết bị từ trên cao.

Và tôi tự hỏi liệu cách này có phát hiện tất cả các thiết bị di động hay không. Trong mã của Tim Stone và naunu, phần đó lớn hơn nhiều.

2

Tương tự như vậy, nếu bạn muốn chuyển hướng đến một thư mục con thay vì một tên miền phụ, làm như sau:

Làm việc tắt của giải pháp tuyệt vời Kevin của bạn có thể thêm video này vào tập tin .htaccess trong thư mục gốc của trang web của bạn thư mục:

<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> 

sau đó, trong thư mục /m/, thêm hoặc tạo một .htaccess như sau:

#Begin user agent loop fix 

     RewriteEngine Off 
     RewriteBase/

    #End user agent loop fix 

tôi biết nó không phải là một ans trực tiếp wer cho câu hỏi, nhưng ai đó (như tôi) có thể vấp ngã khi câu hỏi này và tự hỏi làm thế nào phương pháp đó sẽ được thực hiện là tốt.

+0

điều này là tốt nhất. cảm ơn – dhpratik

-1

Đối với điện thoại di động như 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] 
Các vấn đề liên quan