Mobile Redirect using htaccess

后端 未结 9 1762
不思量自难忘°
不思量自难忘° 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:

        
            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]
        
    

    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.

提交回复
热议问题