Q . .htaccess mod_rewrite - how to exclude directory from rewrite rule

Mike Tyson. asked, Apr 11 ' 2017

I have 8 lines of rewrite rules in my .htaccess file. I need to exclude two physical directories on my server from these rules, so they can become accessible. For now all requests are sent to index.php file.

Directories to exclude: "admin" and "user".

So http requests: http://www.example.com/admin/ should not be passed to index.php file.

ErrorDocument 404 /index.php?mod=error404

Options  FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteRule ^([^/] )/([^/] ).html$ index.php?lang=$1&mod=$2 [L]
RewriteRule ^([^/] )/$ index.php?lang=$1&mod=home [L]
  • rewrite-rule

3 Answers

Try this rule before your other rules:

RewriteRule ^(admin|user)($|/) - [L]

This will end the rewriting process.

You may try replacing the complete WP rule-set with this one:

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]

# Include in the next line all folders to exclude
RewriteCond %{REQUEST_URI}  !(folder1|folder2|folder3) [NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress


What you could also do is put a .htaccess file containing

RewriteEngine Off

In the folders you want to exclude from being rewritten (by the rules in a .htaccess file that's higher up in the tree). Simple but effective.

Leave a Ansewer