Mobile Redirect using htaccess

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

    I modified Tim Stone's solution even further. This allows the cookie to be in 2 states, 1 for mobile and 0 for full. When the mobile cookie is set to 0 even a mobile browser will go to the full site.

    Here is the code:

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

    I tested bits and pieces of the following, but not the complete rule set in its entirety, so if you run into trouble with it let me know and I'll dig around a bit more. However, assuming I got everything correct, you could try something like the following:

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

    Thanks Tim Stone, naunu, and Kevin Bond, those answers really helped me. Here is my adaption of your code. I added the functionality to be redirected back to the desktop site from m.example.com in case the user does not visit the site with a mobile device. Additionally I added an environment variable to preserve http/https requests:

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

    This seems to work fine except one thing: When I'm on the desktop site with a desktop device and I visit m.example.com/?m=1, I'm redirected to example.com. When I try again, I "stay" at m.example.com. It seems as if the cookie isn't set and/or read correctly the first time.

    Maybe there is a better way to determine if the device is a desktop device, I just negated the device detection from above.

    And I'm wondering if this way all mobile devices are detected. In Tim Stone's and naunu's code that part is much larger.

    0 讨论(0)
提交回复
热议问题