Recently I changed the permalink structure on this blog. I updated the .htaccess file in the root directory with the following:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /index.php/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
After that change, suddenly all my password protected sub directories begin to return 404 errors. I tried playing around with different RewriteRules, RewriteCond to see if that would solve the problem, but nothing seemed to work.
Finally, I came across this post. It seems like Apache goes through the following stages when a password protected wordpress sub directory is requested:
- User requests www.exampleblog.com/subdir.
- subdir is password protected, so it sends back a 401 response to ask the user for a username/password.
- The .htaccess file sitting under the root directory processes the response. RewriteRules comes into play and rewrites the URL to www.exampleblog.com
How to resolve this problem?
- Create a dummy html file and put it under the root directory. The file doesn’t need to have any content. Let’s say the file is called onerror.html.
- Towards the top of the root directory’s .htaccess file, add this line:
ErrorDocument 401 /onerror.html
Why does it work? Because once the subdir sends the 401 response, the ErrorDocument directive tells apache to serve the onerrror.html file to the client. The client however won’t display the file because it sees the 401 response in the header and asks the user for the username/password.