The Myriad Blog

Our Online Journal


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.


8 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!

since on June 28, 2011 at 11:47 pm

And the share of fathers living apart from <strong><a href="">Black Annealed Wire </a></strong> children is more than <strong><a href="">galvanized iron wire</a></strong> double what it was just a few decades ago, according to a new study <strong><a href="">barbed wire fence</a></strong>.
But among married fathers, <strong><a href="">PVC Coated Wire</a></strong>children are said to be getting <strong><a href="">Stainless Steel Wire</a></strong> more attention from both parents at home than ever before <strong><a href="">spiral plant supports</a></strong>.
A Pew Research Center report highlights <strong><a href="">welded wire mesh panels</a></strong>the changing roles of parents as US marriage rates <strong><a href="">hexagonal wire netting</a></strong> and traditional family households fall to historic lows <strong><a href="">stainless steel wire mesh</a></strong>.
The study found that <strong><a href="">johnson screens</a></strong> college-educated men who tend to marry and <strong><a href="">Water Well Screen</a></strong> get better jobs are more involved with <strong><a href="">sand control screen</a></strong>their children than lesser-skilled men struggling to get by <strong><a href="">slotted liner</a></strong>.
Pew's survey and analysis of <strong><a href="">wire wrapped screen</a></strong> US government data found that more than <strong><a href="">chain link fences </a></strong> one in four fathers - 27 per cent - with children 18 or younger live away from <strong><a href="">Crimped Wire Mesh</a></strong>at least one of their children. <strong><a href="">pipe production line</a></strong>That number is more than double the share of fathers <strong><a href="">pvc pipe production line</a></strong>who lived apart from their children in 1960 <strong><a href="">pe pipe production line</a></strong>.
But married fathers who <strong><a href="">ppr pipe production line</a></strong> live with their children are actually devoting more time <strong><a href="">plastic sheet extrusion line</a></strong> helping their wives with caregiving at home - <strong><a href="">pe foam sheet extruder</a></strong>a task once seen almost exclusively as a woman's duty. <strong><a href="">twin screw extruder</a></strong> Such fathers on average now spend <strong><a href="">cree LED Flashlight</a></strong>about 6.5 hours a week on child care, <strong><a href="">aluminum led flashlight</a></strong> which include playing, helping children with homework or taking them to activities. That's up from 2.6 hours in the 1960s.
Among fathers with at least a bachelor's degree, <strong><a href="">bright flashlight</a></strong>only 13 percent had children outside marriage, compared to 51 percent of those with high school diplomas and <strong><a href="">dynamo flashlight</a></strong> 65 percent of those who didn't finish high school.
Age, too, was a factor. <strong><a href="">keychain light</a></strong> Three quarters of fathers who were 20 to 24 had children out of wedlock , compared to 36 percent for fathers aged 35 to 44 <strong><a href="h">cree LED Headlamp</a></strong>.
The findings come as the latest US census data show that marriages have fallen to a record low, <strong><a href="">led work light</a></strong> pushing the share of US households with married couples below 50 percent for the first time.
Sociologists say younger people are <strong><a href="">led camping lantern</a></strong> increasingly choosing to live together but delay marriage as they struggle to find work and resist making long-term commitments.

Kaylene on November 17, 2011 at 6:52 pm

Your post was a life saver. My server was returning Error 404 with nothing helpful in the error_log when I tried to enable basic authentication in a protected directory. I spent days researching and trying to figure out what was happening. Your solution worked, and the legacy software is now up an running. Thank you again!

Michael Robinson on July 29, 2012 at 7:53 pm

Thank you for posting this, I had a very frustrating time trying to get auth + rewrites working this morning.

Referenced your post here: <a href="">Stackoverflow</a>.

fix on June 3, 2013 at 12:23 am

awesome, this did help a lot.

Todd on January 7, 2015 at 1:45 pm

Thanks! Setting the error document fixed it for me. For the life of me I couldn't figure out why the rewrites were still taking place even though access was denied. It makes sense now.

Leave a Reply

Security Code

Recent Projects
Our Services
Follow Us