How to exclude a sub-folder from HTaccess RewriteRule

10,500

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.

Share:
10,500

Related videos on Youtube

user250572
Author by

user250572

Updated on September 17, 2022

Comments

  • user250572
    user250572 over 1 year

    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?

    • Prix
      Prix over 13 years
      I was talking in the #httpd chat another day and ($|/) does not seem to fit the idea so it should be: RewriteRule ^(blue$|blue/) - [L] I also tried some stuff later after it and it seems to be the case. Also make sure you put this rule at the at the begin and not after your other rules so it takes places as soon as it hits
    • Prix
      Prix over 13 years
      In ragards to the .htaccess, if you have one at a top level directory, it will overwrite any others. For example, root directory is /home/domain/public_html and there you have a .htaccess then you have /home/domain/public_html/forum with another .htaccess the later would be ignored.
  • user250572
    user250572 almost 14 years
    Well it's showing a 404 within WordPress (e.g. the WP site in the root shows a 404 message in the center), meaning the RewriteRule is being applied and it's hitting WP's index.php, which serves a 404.
  • matthew
    matthew almost 14 years
    Correct: It appears that it goes through the root .htaccess fine, then hits the /blue .htaccess and gets rewritten, then when it gets passed through the root .htaccess a second time it no longer gets excluded by the RewriteCond and thus gets rewritten to the WP index.php... or at least that's what I'm guessing is happening.