John Kilbey
John Kilbey

Reputation: 35

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]

Upvotes: 0

Views: 84

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133538

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]

Upvotes: 1

Related Questions