Mobile Redirect using htaccess

后端 未结 9 1652
不思量自难忘°
不思量自难忘° 2020-11-22 03:36

I have a website called

www.website.org

I have a mobile website called

m.website.org

I want to use an htaccess to a

相关标签:
9条回答
  • 2020-11-22 04:01

    Similarly, if you wanted to redirect to a sub-folder instead of a sub-domain, do the following:

    Working off of Kevin's great solution you can add this to the .htaccess file in your site's root directory:

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

    Then, in the /m/ folder, add or create an .htaccess with the following:

        #Begin user agent loop fix
    
             RewriteEngine Off
             RewriteBase /
    
        #End user agent loop fix 
    

    I know it's not a direct answer to the question, but somebody (like me) might stumble upon this question and wonder how that method would be accomplished as well.

    0 讨论(0)
  • 2020-11-22 04:11

    Tim Stone's solution is on the right track, but his initial rewriterule and and his cookie name in the final condition are different, and you can not write and read a cookie in the same request.

    Here is the finalized working code:

    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]
    
    0 讨论(0)
  • 2020-11-22 04:13

    You can also try this. Credits to the original author who has since removed the script

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

    Just edit the $mobile = "http://www.stepforth.mobi";

    0 讨论(0)
  • 2020-11-22 04:16

    First, go to the following URL and download the mobile_detect.php file:

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

    Insert the following code on your index or home page:

    <?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");
    }
    ?>
    
    0 讨论(0)
  • 2020-11-22 04:17

    Or you may try this:

    ?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;
    }
    
    0 讨论(0)
  • 2020-11-22 04:21

    For Mobiles like 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]
    
    0 讨论(0)
提交回复
热议问题