'Can't exclude directories from .htaccess mobile redirect?

I'm using the .htaccess mobile redirect below which I found on an older post here. It works great, but I need to exclude several directories from the redirect rule.

I've tried:

RewriteRule !^your-directory($|/) http://www.example.com%{REQUEST_URI} [L,R=301]

Which kills the directory.

And:

RewriteCond %{REQUEST_URI} "/your-directory/"

Which does nothing.

I tried placing them in different places also.

What code and where do I put it. I simply need these directories to be unaffected by the rewrite rules, or even the whole root .htaccess file.

This is what I'm using.

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


Solution 1:[1]

Your second RewriteCond is almost there I think. Try something like this:

RewriteCond %{REQUEST_URI} !^/?(directory1|directory2)($|/)

Place as many directories as you need in directory1|directory2|etc.

This should come out to "if REQUEST_URI does not begin with /directory1 or /directory2."

It will match:

  • /directory1/dasdas
  • /directory1
  • /directory1/

etc

Which I think is what you want.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matt Blaha