Вопрос

How to redirect to www.example.com when accessing www.example.com?mobile=0 after the cookie is set?

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

What this code does is to redirect www.example.com to m.example.com if accessed from a mobile device, but not when accessing www.example.com?mobile=0 which will set a cookie to keep the user on the main site even on a mobile device.

I would like to add a 302 redirect from www.example.com?mobile=0 to the clean URL www.example.com once the cookie preventing the redirection is set.

Это было полезно?

Решение

Append this rule at the end of your existing rules:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?mobile=0[\s&] [NC]
RewriteRule ^ /? [R=302,L]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top