The Myriad Blog

Our Online Journal

07
May
2009

Conflict Between Apache URL Rewriting and Basic Authentication

| Posted by Peter Schultheiss on May 7th, 2009 at 04:04 pm

The Apache module mod_rewrite provides a rule-based rewriting engine that is used by most MVC frameworks (i.e. CakePHP and Zend Framework) and content management solutions (i.e. Drupal and WordPress.)

The benefits of using a rewrite engine include:

  • Making website URLs more memorable to users
  • Making website URLs more relevant to search engines
  • Masking the inner workings of the site by hiding file extensions

I ran into an issue the other day where I needed to run a legacy application that was being protected by basic .htaccess authentication. The legacy application was located in a subdirectory.

Typically, you would just add the following line of code to your .htaccess file to instruct the rewrite engine to ignore the directory:

RewriteEngine off

Unfortunately, this won't work as expected. For basic authentication, the server writes a "401 Unauthorized" header and then looks for an error document based on a pre-defined path. Most of the time, the error document won't exist in the directory that you want to protect, so the request gets handled by the rewrite engine which throws a 404 error.

The solution to this problem is pretty straightforward. You need to add a single line of code to your .htaccess file instructing Apache to ignore the error document. When you're done, the code should look something like this:

ErrorDocument 401 "Unauthorized Access"

RewriteEngine off

AuthType Basic
AuthName "Administration"
require valid-user
AuthUserFile "/home/user/.htpasswds/public_html/admin/passwd"

Hopefully this will save somebody a bit of aggravation.

Comments

3 responses to "Conflict Between Apache URL Rewriting and Basic Authentication"

NickC on December 8, 2009 at 3:19 pm

Thank you....

voiture thermique on April 6, 2010 at 10:34 pm

Hey, thank you for this info i'm on the precess of getting a new domain. Thanks again.

Trent on July 19, 2010 at 6:17 am

Life saver! I was beginning to think I'd never get to the bottom of this conflict, I just couldn't make sense of it... Thanks!

Leave a Reply

Security Code

Recent Projects
Our Services
Follow Us