RewriteCond to exclude a path that does not work.

I want this URL:

http://www.example.com/path/antyhing 

not to be redirected.

Here is what I have that doesn't work:

 RewriteCond %{REQUEST_URI} !^/path/.*$ [NC] RewriteRule ^ http://m.example.com/ [R,L] 

It currently redirects all URLs to http://m.example.com/

Here is the complete code in my .htaccess file:

 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}] # Skip next rule if mobile=0 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(;|$) # Don't redirect "path" pages RewriteCond %{REQUEST_URI} !^.+?/path/.*$ [NC] # Now redirect to the mobile site RewriteRule ^ http://m.example.com/ [R,L] 
+1
regex url-rewriting .htaccess mod-rewrite
Sep 18 '13 at 11:42 on
source share
1 answer

Can you replace your last rule with the following:

 Options +FollowSymLinks -MultiViews # Turn mod_rewrite on RewriteEngine On RewriteBase / # 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}] # Skip next rule if mobile=0 [OR] if it a file [OR] if /path/ RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$) [OR] RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_URI} ^.*/path/.*$ RewriteRule ^ - [S=1] # Check if this looks like a mobile device RewriteCond %{HTTP_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] # 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(;|$) # Don't redirect "path" pages RewriteCond %{REQUEST_URI} !^.*/path/.*$ [NC] # Now redirect to the mobile site RewriteRule ^ http://m.example.com/ [R,L,NC] 

Edit by OP: The only problem came from %{REQUEST_URI} , which, for the reason that I do not understand, only works with ^.*/path/.*$

+3
Sep 18 '13 at 12:17
source share



All Articles