Hide some directories from WordPress without breaking WordPress 404 messages

I’m trying to block WordPress from messing with /example and /example2, as I had some advanced things (e.g. password protection) that WordPress was breaking and incorrectly giving 404 messages to.

Here’s my .htaccess:

Read More
DirectoryIndex index.html index.php parking-page.html

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

# END WordPress

This works to block WordPress from those folders, but now WordPress won’t catch bad URLs anymore unless they have a / in them.
For example, WordPress will catch:

mydomain.com/something/
mydomain.com/something/anything
mydomain.com/something/anything.html

but it won’t catch

mydomain.com/something
mydomain.com/something.html

(even if mydomain.com/something is a valid WP page that would normally work if I didn’t have the %{REQUEST_URI} line in there)

The latter type of URL is given the server 404 messages. How can I keep WordPress in the loop on everything but those two directories?

Related posts

Leave a Reply

1 comment

  1. I have discovered the solution (hat-tip). It is the exact same issue as on this question, except whereas his problem was that the directory in question was using WebDAV, my problem was that the directory was using password protection.

    In essence, the default WordPress rules should work as they are–but they break when the folder is password protected. These rules that should catch it as a file (-f) or directory (-d) fail:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    

    It wasn’t catching my password protected directory as a file or directory because it was stuck at the password prompt–and I didn’t have any error pages to send it to. Thus, the solution is to set up error handling:

    ErrorDocument 401 /err.txt
    ErrorDocument 403 /err.txt
    

    Just put these two lines above all the WordPress stuff and create /err.txt. That way it can land on the file and the default WP code works beautifully.