My Guy Solutions

Using WordPress’s permalink feature seems to cause some issues with password protected directories that use Apache’s .htaccess to handle authentication. I recently had to troubleshoot why after installing WordPress on the root level of the domain that a password protected directory would return a 404 page instead of the typical login box.

The problem comes from the Apache rewrite engine that WordPress uses to make search engine friendly URL’s. WordPress uses a .htaccess file in the root folder of the install to take any URL and allow WordPress to process and serve the appropriate page, or error.

What Exactly Is Breaking User Logins

In this condition the Web server thinks that the HTTP data stream sent by the client (ex. a web browser) was correct, but access to the URL requires user authentication which has not yet been provided or which has been provided, but failed authorization tests. This is commonly known as “HTTP Basic Authentication” or HTTP Error 401 Unauthorized.

Generally this error message means you need to log on (enter a valid user ID and password) somewhere first. If you have just entered these and then immediately see a 401 error, it means that one or both of your user ID and password were invalid for whatever reason (entered incorrectly, user ID suspended etc.).

Let’s Fix Directory Listing Too

Another issue could be a HTTP Error 403 Forbidden. The Web server thinks that the HTTP data stream sent by the client (ex. your web browser) was correct, but access to the resource identified by the URL is forbidden for some reason. The most common reason for this error is that directory browsing is forbidden for the Web site. Most Web sites want you to navigate using the URLs in the Web pages for that site. They do not often allow you to browse the file directory structure of the site.

Understanding What it All Means

Now that we know what is causing the errors it is time to fix them. We will perform the solution by placing a couple of ErrorDocument handlers at the top of the .htaccess to pre-empt the WordPress .htaccess rules.

There are a couple of scenarios we will be preventing WordPress from handling. If there are 401 errors (directory authentication) it will send the user to an error page. If there is a 403 error code (a forbidden directory situation) it will send the user to an error document as well. The WordPress permalinks rule never gets processed and will be ignored for these two errors.

Editing the WordPress .htaccess is Simple Solution

The first thing you need to do is locate your WordPress directory. This is often either your www directory or public_html directory. Locate your .htaccess file and open it using any text editor. If you do not see this file you can simply create one using any text editor (such as Notepad on Windows).

Update the .htaccess file before the WordPress information and add the following two lines of code:

ErrorDocument 401 ./error.html
ErrorDocument 403 ./error.html

The finished .htaccess should look like:

ErrorDocument 401 ./error.html
ErrorDocument 403 ./error.html
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

Victory!

Make sure your FTP program is set to upload as ASCII. Upload the .htaccess file to your home directory (often public_html or www). You can also upload an error.html to the root directory.

Attempt to access a directory that is password protected will now give you the popup box you were expecting in the first place, so you can enter your login credentials and gain access as normal.

We also learned how to process 401 and 403 errors in the process using Apache’s ErrorDocument directive using .htaccess in the process.

Tags: , , , , , , , ,

15 Responses to “How To: Fix WordPress 404 Errors on Password Protected Directories”

  1. Dave says:

    Thanks. Just what I needed and had been looking for over a month.

  2. Taggart says:

    Yep O This is Just what I needed … My WordPress was installed in the root /public_html folder so website.com/
    but I also had a folder in there called members …that was password protected
    by its own .htacces folder:

    I then Found this Lovely Bit of info :)
    and just copied: To my roots .htaccess folder i found it a bit strange because there is no such file as error.html …but what ever
    IF IT WORKS IT IS RIGHT

    ErrorDocument 401 ./error.html
    ErrorDocument 403 ./error.html
    # BEGIN WordPress

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPress

    THANKS AGAIN !

  3. Thank you! A nice and simple solution to a problem that’s been bugging me for two nights now. It was surprisingly hard to come by solid information on the subject. You also helped me tidy up my .htaccess file and gained a new subscriber. :)

  4. Mohammad Naji says:

    Thank you a lot

    You really solved my problem!

  5. Tom says:

    I searched “password protected directories 404″ (no quotes) in Google. Your article is #1 and I am glad of it.

    I have a fair number of sites floating’ around these days. Couldn’t figure out why the password protect feature wouldn’t work for some.

    It was the WordPress in the root causing what I assumed was a 404.

    Thank You for the enlightenment and the solution!

  6. Bill P. says:

    Your fix worked perfectly on my WordPress site. I don’t know how you figure this stuff out but I’m glad you’re generous with your knowledge and time.

    I wasted hours “trouble shooting” the problem until I looked for help and found your site. Very nice!

    Thank you so much.

    Bill P.

  7. I am so grateful for your instructions. There are clear and very professional. It solved the problem completely. I wish you a creative week ahead.

    Warm wishes,

    Antti Haverinen

  8. niraj says:

    i have my website with permalink, till now everything was fine, but few days before i changed my permalink, now everything is going wrong, now each and every page i am getting 404, even my about-us and contact us pages, they are giving me 404 page if i come to my site from google or any search engines, then also it gives me 404 i have 1000+ posts on my website, for each post & page from google i get 404 page, but within the website except pages i get no 404 page my previous permalink was, %postname%/%category%/

    now i added custom taxonomy to my permalink, so it is %location%/%courses%/%postname%
    how can i change ?????

  9. haseeb says:

    awesome..works like a charm

  10. For me, don’t work.
    Really ask for login and password, but always said Login/pass incorrect.

  11. The article has been of great use for me.
    I wasted almost 5 hours trying to find out myself as to why was I getting Page Not Found error

    Thanks again

  12. Diseño| Hosting| Computadoras…

    [...]How To: Fix WordPress 404 Errors on Password Protected Directories « My Guy Solutions – Web development, PHP, Web Design, E-Commerce, Denver, Colorado Springs[...]…

Leave a Reply