WordPress 2, .htaccess Protected Directory, and 404 Error

作者: , 2006/03/17 17:01 EST

Note: I found an even better solution. Please see my comment below.

Ever since upgrading this blog from WordPress 1.5 to 2.0, files inside a password protected directory no longer works. Any attempt to load file in the directory will be redirected to WordPress and get a 404 error. After some research and debugging, I find the rewrite section WordPress put in my main .htaccess file is causing the problem. The rules reads like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

What it does is check to see if the requested filename is a regular file or a real directory. If it’s neither of the two then redirect to /index.php, which is WordPress entry.

Supposedly, any existing directory or file in it will fail the test. So Apache will load the file/directory instead of WordPress index.php. However, the fact that the requested directory is password protected with its own per-directory .htaccess file seems to cause Apache to think it’s not really a directory/file, thus satisfying the 2 tests and invoke WordPress index.php.

I tried to change my main .htaccess file to fix that. But couldn’t. And I am not alone. I find many people with similar problem in WordPress support forum. For example: WP 2.0 – htaccess overly aggressive? 404’s galore

Now I finally deviced a fix to it and it’s to change the index.php file WordPress is using. Since my WordPress install is inside a subdirectory in my web root, I have a copy of the index.php in my root directory while loads WordPress files from the subdirectory. So changing it is fine for updating WordPress won’t overwrite it.

Here’s the fix:
/* Short and sweet */
$request_filename = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
$this_dir = dirname(__FILE__).'/';
if($request_filename!=$this_dir && $request_filename!=__FILE__ && (is_file($request_filename) || is_dir($request_filename))) {
  // we are not supposed be here!
// load WordPress in /wp
define('WP_USE_THEMES', true);

What it does is first make sure the request is not for the home directory or index.php itself. We want WordPress to handle these, right?

Then it tests if the request is a regular file or a real directory. And die if that’s the case. Basically we are doing what the two RewriteCond lines in .htaccess supposed to do. Magically, this works!

Now, my password protected directory correctly asks for password instead of showing WordPress 404 error page. All WordPress permalinks work. All non-protected directories work. Direct URLs to my Gallery2 work. It all works. That’s a rare thing to say about anything I did. Really! Just ask my wife about the house… Err, wait. No, don’t do that.

I don’t know if this would work with other setup, like WordPress is installed right in the web root. But I guess it should. So if you have the same problem, give it a try and let me know if it helps. Good luck.

「WordPress 2, .htaccess Protected Directory, and 404 Error」有 34 個回應

  1. brasilblogger表示:

    Oh, to the comment Mário Gamito a tip … so your webserver shows not up php error … but there are some in the code … you have to use always ‘ instead of ’

    for example here:
    $this_dir = dirname(__FILE__).’/’; <<< wrong
    $this_dir = dirname(__FILE__).’/’; <<< right

    define(’WP_USE_THEMES’, true); <<< wrong
    define(‘WP_USE_THEMES’, true); <<< right

    require(’./wp/wp-blog-header.php’); <<< wrong
    require(‘./wp/wp-blog-header.php’); <<< right

    if wordpress is on root:

    Try it out. Hope it helps.

  2. brasilblogger表示:

    Ok, the comments can’t display this. ;(
    So use always the right apostroph >> Alt+39 to find on the keyboard over the #

  3. brasilblogger表示:

    Haha! And I thought, that I can’t copy & paste anymore. Because after my second comment, the code above in your posting was correct … *ups*

    …. so edit/delete my comment with the wrong quotes … 😉

  4. Liane表示:

    It worked! The error doc fixed didn’t work for me – I’ve got my index.php in the root and WordPress in another directory. Funny, I used the error doc fix yesterday, and it worked, but was a temporary solution, didn’t work at all after that.

    Glad I found this article! Bookmarked…


  5. 朱朱表示:

    Liane. Glad that this old trick is still helpful. Enjoy.

  6. […] it works, but once I reboot, I get the same 404 problem. However, I've found found a solution at ju-ju.com's WordPress 2, .htaccess Protected Directory, and 404 Error. While the author has pointed to a solution in a comment, his own solution (editing index.php) has, […]

  7. […] I worked it out by asking on Site5′s forum and got the solution from this site. Because I installed WordPress in a sub-directory /blog under my root, so the solution is to modify […]

  8. […] I worked it out by asking on Site5′s forum and got the solution from this site. Because I installed WordPress in a sub-directory /blog under my root, so the solution is to modify […]


Panorama Theme by Themocracy