朱家兩大兩小在美國的生活 The Chus from the East

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:
<?php
/* 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!
  die;
}
// load WordPress in /wp
define(’WP_USE_THEMES’, true);
require(’./wp/wp-blog-header.php’);
?>

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.

RSS feed/Trackback URI

“WordPress 2, .htaccess Protected Directory, and 404 Error” has 19 comments.

  1. Solution for Password Protected Directories and WordPress - WordPress SEO and Blog Marketing - 發表於2007/6/13 星期三 02:46:15 EDT。
  1. 朱朱
    2006/3/17 星期五 18:47:50 EST

    Found a even better solution at Textpattern:

    http://textpattern.com/faq/173/password-protected-directories-with-htaccess

    Basically, the problems is partly caused by incomplete error document set up. Specifying error documents in .htaccess file fixed my problem. But the hack in the original post might still be of use in other situations.

    So scrap the above exercise. Or just pick your poison.

  2. Billy
    2006/9/28 星期四 15:13:19 EDT

    Thank you!!! I’ve spent hours trying to figure out what was wrong!

  3. 朱朱
    2006/9/28 星期四 15:32:42 EDT

    No problem. Glad to be of help.

  4. Patricia
    2006/11/10 星期五 17:25:04 EST

    Thank you!!!! I was having the exact same problem with one of my blogs and your solution worked right away. Much obliged!

  5. Nathan Logan
    2007/4/11 星期三 16:53:57 EDT

    Hey, just wanted to say thanks for this solution. Even though I’m using Textpattern, the linked solution didn’t work out for me. So after battling with (and losing to) .htaccess, I implemented something like you have here.

    Thanks for the pointer!

  6. 朱朱
    2007/4/11 星期三 17:31:30 EDT

    Nathan. You are welcome and glad that it works for you. Have fun!

  7. jayne d'Arcy
    2007/6/10 星期日 04:03:17 EDT

    I was losing my hair over this maddening problem. I tried something similar to the textbook solution, but that didn’t work for me. My blog is in the root, so if that has something to do with it… I don’t know. I’m far from any sort of expert. However, I implemented your index.php modification just a few minutes ago and now my test subdirectory password protect is working. It also looks like the blog is continuing to work fine, with the pretty permalinks. I’ll give it a few more days and come back if anything goes wonky. Thanks, though, for this fix.

  8. 朱朱
    2007/6/10 星期日 09:41:55 EDT

    Jayne. I am glad the tricks work for you.

    PS. I thought your comment was a spam at first when I read something about hair loss. :)

  9. Dave
    2007/8/9 星期四 01:30:34 EDT

    Hrmm…I’ve tried doing the index.php mod that you suggested and it breaks my WP index.php. All other sections of WP work fine, but index.php loads empty. If I do this, my protected directory then works properly (but, I’m more concerned with the index.php loading than I am a protected directory - obviously!).

    I’m at a loss…if I switch back (which I have done), I regain functionality of WP, but I lose pw-protected doc to the 404 error.

    *sigh*

  10. 朱朱
    2007/8/9 星期四 01:34:47 EDT

    Dave. Have you tried the HTML error document fix in the first comment?

  11. Dave
    2007/8/9 星期四 02:16:20 EDT

    I tried a bunch of variations…I was so close, but forgot to fix my error paths back so it was not reading properly. Just got it fixed now - many thanks!

  12. Chester
    2007/12/28 星期五 18:27:20 EST

    With WP installed in my root, I could not get the simple Error redirect fix to work. I had to use your index.php fix for this to be resolved properly.

  13. 朱朱
    2007/12/28 星期五 20:22:58 EST

    That’s cool. I knew my ugly hack has to be useful somehow. :)

  14. ismael
    2008/2/29 星期五 10:33:45 EST

    great! works perfect!!

    many thanks!

    ismael.

  15. 朱朱
    2008/2/29 星期五 11:04:06 EST

    Hi ismael. Glad that it works for you.

  16. Henriemedia.com
    2008/5/23 星期五 15:18:12 EDT

    Thank you for the PHP code! I could’t get the .htaccess changes to work, but your code works like a dream for my Wordpress nightmare!!! Thank you!!!

  17. MBC
    2008/8/8 星期五 12:56:25 EDT

    Thanks for the solution, you saved me from going crazy and closing down the blog :P

  18. 朱朱
    2008/8/8 星期五 13:33:02 EDT

    You are welcome. Glad to be of help.

Leave A Comment

(由於反制垃圾留言的功能,你的回應可能不會馬上出現。)


完整 RSS回應 RSS
東方之朱是以 WordPress 建構。原始主題設計:Matthew0.809
登入][WebMail