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:
www.mydomain.com/mydir/somefile.html
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?
Should still work. Can you paste an example of your htaccess and directory you’re trying to access?
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
I just tested it and it worked fine. Created a test directory /secure/ and put test.html file in it. it’s viewable in the browser:
https://tanyanam.com/secure/test.html
Do you have correct directory permissions (755) on /secure?
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!
Great, glad it worked! :)
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
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
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
Did not work:
RewriteCond %{REQUEST_URI} !^/(secure|secure/.*)$
Did work:
RewriteCond %{REQUEST_URI} ^/secure/(.*)$
But the rest of the site won’t work now. Wonder how I could combine the two…
This is working for now…
# RewriteRule . /index.php [L] # Original line
RewriteRule ./ /index.php [L] # New line
Hi Tanya,
I am looking for something opposite of this for example, I have a site which is on wordpress I have a custom front page and a separate blog page but I want to show all my blog posts something like this:
http://www.example.com/my-blog-post
instead of
http://www.example.com/blog/my-blog-post
How could I rewrite the rules for the above solutions. Thanks in advance.
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.
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]
sunil thank you very much, your code work very well!
What if WordPress is installed in a subdirectory itself? (i.e. RewriteBase /wordpress/) What should the RewriteCon look like?
Pingback: WordPress: remover diretório da regra URL rewrite pelo .htaccess | Taylor Lopes ツ Blog
Pingback: Excluding a directory from WordPress // Jeremy Carlson
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.
Thank you for this. I am having a problem with paypal IPN notifications, they only work when I have pretty permalinks switched off. I was wondering if I could exclude
/?wc-api=WC_Gateway_Paypal
as it’s being rewritten as /wc-api/WC_Gateway_Paypal/
by using this rule and how should I write it?
Many thanks in advance, Ana
I was wondering if I could use this rule to exclude ?wc-api=WC_Gateway_Paypal
as it’s being rewritten as wc-api/WC_Gateway_Paypal/ and my paypal IPN notifications won’t work as they should.
Many thanks, Ana
Sorry I wrote that twice, I didn’t think the first message had been sent. :)
Hi
I need to use this too, I did so far – but all css and js files are not accessible: https://www.samuiislandexplorer.com/transfer/
as it put’s a “/” at the end. Any idea how to do it?
Regards,
Simon
No idea without more explicit code to look at. But: if the exclusion is working properly, then when you are viewing that directory, the rewrites shouldn’t be happening. You should be able to pull in CSS & JS assets however you want on those pages; WordPress shouldn’t have anything to do with it.
Pingback: Excluding a directory from WordPress – Geniuswp