WordPress: Exclude directory from URL rewrite with .htaccess

As you know, WordPress uses .htaccess to rewrite URLs so they become nice and SEO-friendly.

Sometimes though, you might want to use a directory within your web root that can be accessed directly.

To do this, open your .htaccess file and add a line right under the first RewriteRule instruction:

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

Replace “mydir” with your actual directory name.

So your full .htaccess instruction set will look like this:

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

Now you should be able to access that directory directly, like this:

http://www.mydomain.com/mydir/somefile.html

Advertisements

20 thoughts on “WordPress: Exclude directory from URL rewrite with .htaccess

  1. Jack McKor

    Does this still work? Tried it on one of my sites and I’m still getting a 404 error when trying to access a directory?

  2. Jack McKor

    I’m trying to access files in /secure of my website.

    Here’s what I’m using:
    # BEGIN WordPress

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.php$ – [L]
    RewriteCond %{REQUEST_URI} !^/(secure|secure/.*)$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPress

  3. Jack McKor

    Hi Tanya,

    Yeah I found out that that was the problem –
    my permissions were set to 777 for some
    odd reason.

    It works perfectly now. Thanks for your help!

  4. Hi,
    I have similar issue, I have wordpress installation on root folder. and i have tool with java script on a subfolder called smart inside the root folder.

    i my site http://www.jump2health.com/smart, the page doesn’t show up all the java script it is supposed to. where as i have uploaded it in a different place (http://billmath.com/smart/) and it works as expected. when i rename (remove) the .htaccess file which is in root the java script works as expected , however this stops my word press site from working normally.

    I tried the solution you mentioned here but this doesn’t solve my problem. and my folder has 755 access rights what else am i missing here

    1. Tanya

      Hi Yugi,

      Looks like a different issue, if javascript is not working on the page. The page itself seems to load just fine, so .htaccess is configured fine.

      I would try checking javascript console and make sure your paths to .js files are correct

  5. Hi Tanya,

    My problem is that I’m trying to use .htaccess to password protect the excluded directory under by WordPress site with .htaccess and .htpasswd files *inside* that excluded directory:

    eg, http://www.centerforward.com/test should popup a user/pass login.

    If I create /test off of centerforward.com (/home/alan/www/test), then I can hit http://www.centerforward.com/test directly, and without a 404 error actually (?).

    And, when I use the same .htaccess and .htpasswd method I’ve used for a decade, it doesn’t work: no user/pass pops up. But, it directly hit /test and load that directory listing without a ‘404 not found’.

    But as soon as I put a .php in there, ie /home/alan/www/test/index.php (), THEN I get the 404 not found inside my centerforward.com WordPress site framework.

    And this didn’t change that:

    # BEGIN WordPress

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/(test|test/.*)$
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPress

    My linux admins are telling me this is outside the scope of that they can help me with (which is really lame), is there anything you could suggest! Or do you have an affordable linux admin you might recommend for such issues!!

    Thanks so much for your great article!,
    Alan

    1. Tanya

      Umair, did you try looking at WordPress Permalink settings? Perhaps you have /blog/ as part of your permalink structure (as in this example). In this case you can just remove it. Here’s an example of what I have, without /blog directory:

      If it isn’t, then you can look into rewrites, and your regular expression could be this.

  6. Hello,

    Below code is working fine.

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} ^/dir/(.*)$
    RewriteRule ^index.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

  7. Steve

    What if WordPress is installed in a subdirectory itself? (i.e. RewriteBase /wordpress/) What should the RewriteCon look like?

  8. Pingback: WordPress: remover diretório da regra URL rewrite pelo .htaccess | Taylor Lopes ツ Blog

  9. Pingback: Excluding a directory from WordPress // Jeremy Carlson

  10. Tanya, thank you—this was super-helpful!

    I ended up wanting to ensure the code would stay even if I flushed the rewrite rules by changing/saving permalinks, so I ended up hooking a function to the mod_rewrite_rules filter. I wrote this up at https://jeremycarlson.com/exclude-a-directory-from-wordpress/

    Steve, to answer your question re having WP in a subdirectory, the RewriteCond shouldn’t really be any different—that’s what I’m doing w/ all my builds.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s