How to exclude a sub-folder from HTaccess RewriteRule

.htaccessapache-2.2password-protectedrewriterewritecond

I have WordPress installed in my root directory, for which a RewriteRule is in place.

I need to password-protect a subfolder ("blue"), so I set the htaccess in that folder as such.

Problem is that the root htaccess RewriteRule is applying to "blue" and thus I get a 404 in the main WordPress site (instead of opening the password dialog for the subfolder).

Here's the root htaccess:

RewriteEngine on

<Files 403.shtml>
order allow,deny
allow from all
</Files>     

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

I tried inserting this as the second line, to no avail:

RewriteRule ^(blue)($|/) - [L]

Also tried inserting this before the index.php RewriteRule:

RewriteCond %{REQUEST_URI} !^/blue/

That didn't work either.

Also inserted this into the subfolder's htaccess, which didn't work either:

<IfModule mod_rewrite.c> RewriteEngine off </IfModule>

Any ideas?

Best Answer

Try dropping the leading / from the RewriteCond

RewriteCond %{REQUEST_URI} !^blue/

EDIT: It should work without the RewriteCond as the first two conditions should cover it unless you're doing more rewrites in that directory or something.

Are you using custom error pages? Something is changing the Request_URI so that it no longer matches any of the conditions.

If you're not on a shared host or if your host supports it try setting up a rewrite log this will tell you exactly what is going on.

Related Topic