Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

.htaccess - HTACCESS REDIRECT TO mobile site but need to exclude one directory

Im using the htaccess code below to redirect anyone on a mobile device to the mobile version of the website but I need to exclude the folder stats How can I do this

RewriteBase /
# Check if this is the m=no query string
RewriteCond %{QUERY_STRING} (^|&)m=no(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:%{HTTP_HOST},S]
# Check if this looks like a mobile device
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
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} ^(?:www.)?website.co.uk$
#Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=no(&|$) 
#Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]
#Now redirect to the mobile site
RewriteCond %{REQUEST_URI} !.(?:gif|jpe?g|png|pdf)$ [NC] 
RewriteRule ^ http://m.website.co.uk%{REQUEST_URI} [R,L]
question from:https://stackoverflow.com/questions/65858376/htaccess-redirect-to-mobile-site-but-need-to-exclude-one-directory

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

With your shown rules, could you please try following. In case you are looking for a uri which starts from stats then change FROM stats TO ^/stats in following rules.

RewriteEngine ON
RewriteBase /
# Check if this is the m=no query string
RewriteCond %{QUERY_STRING} (^|&)m=no(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:%{HTTP_HOST},S]

# Check if this looks like a mobile device
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
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} ^(?:www.)?website.co.uk$
#Can not read and write cookie in same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=no(&|$) 
#Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]
#Now redirect to the mobile site
RewriteCond %{REQUEST_URI} !.(?:gif|jpe?g|png|pdf)$ [NC] 
RewriteCond %{REQUEST_URI} !stats [NC]
RewriteRule ^ http://m.website.co.uk%{REQUEST_URI} [R,L]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...