Exclude WordPress directory from URL rewrite with .htaccess

I ran across a situation today where I had a WordPress site that had a directory for webmaster tools. The problem was that the .htaccess file for the WordPress site was set up to redirect all pages to a WordPress post or page, so if you tried to go to example.com/blog/tools/tool.php, it wouldn’t allow that. Here is the fix:

Add a Line to .htaccess to Exclude a Directory from WordPress Redirects:

Basically your WordPress section of the .htaccess file will look similar to this:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_URI} !^/(diamondsearch|diamondsearch/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

So what you do if find the first “RewriteRule” line and add this line below it:

RewriteCond %{REQUEST_URI} !^/(dirName|dirName/.*)$

Simply replace both instances of “dirName” with your directory name that you want to be excluded from WordPress redirects. Your final .htaccess file will look something like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_URI} !^/(dirName|dirName/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

One Reply to “Exclude WordPress directory from URL rewrite with .htaccess”

  1. Doesn’t work for me. But then nor do any of the umpteen suggestions I’ve tried off stackoverflow. Even though it was simple enough on another WP site to ignore a particular directory with
    RewriteRule ^directory/ directory/index.php [L]
    RewriteRule ^directory/(.*) directory/$1 [L]

    What could be the difference? A later version of WP?

Leave a Reply

Your email address will not be published. Required fields are marked *