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

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="http://www.hengyangcompany.com/p-metal-wire-99091/Black-Annealed-Wire-106840/">Black Annealed Wire </a></strong> children is more than <strong><a href="http://www.hengyangcompany.com/p-metal-wire-99091/Galvanized-Iron-Wire-106842/">galvanized iron wire</a></strong> double what it was just a few decades ago, according to a new study <strong><a href="http://www.hengyangcompany.com/">barbed wire fence</a></strong>.
But among married fathers, <strong><a href="http://www.hengyangcompany.com/p-metal-wire-99091/PVC-Coated-Wire-103358/">PVC Coated Wire</a></strong>children are said to be getting <strong><a href="http://www.hengyangcompany.com/p-metal-wire-99091/Stainless-Steel-Wire-106841/">Stainless Steel Wire</a></strong> more attention from both parents at home than ever before <strong><a href="http://www.hengyangcompany.com/p-metal-wire-99091/Spiral-Plant-Supports-103377/">spiral plant supports</a></strong>.
A Pew Research Center report highlights <strong><a href="http://www.hengyangcompany.com/p-Wire-Mesh-103486/Welded-Wire-Mesh-Panels-103508/">welded wire mesh panels</a></strong>the changing roles of parents as US marriage rates <strong><a href="http://www.hengyangcompany.com/p-Wire-Mesh-103486/Hexagonal-Wire-Netting-103509/">hexagonal wire netting</a></strong> and traditional family households fall to historic lows <strong><a href="http://www.hengyangcompany.com/p-Wire-Mesh-103486/Stainless-Steel-Wire-Mesh-103510/">stainless steel wire mesh</a></strong>.
The study found that <strong><a href="http://www.hengyingwiremesh.com/">johnson screens</a></strong> college-educated men who tend to marry and <strong><a href="http://www.hengyingwiremesh.com/p-Wedge-Wire-Screen-104558/Water-Well-Screen-104563/">Water Well Screen</a></strong> get better jobs are more involved with <strong><a href="http://www.hengyingwiremesh.com/p-Wedge-Wire-Screen-104558/Sand-Control-Screen-104562/">sand control screen</a></strong>their children than lesser-skilled men struggling to get by <strong><a href="http://www.hengyingwiremesh.com/p-Wedge-Wire-Screen-104558/Slotted-Liner-104561/">slotted liner</a></strong>.
Pew's survey and analysis of <strong><a href="http://www.hengyingwiremesh.com/p-Wedge-Wire-Screen-104558/Wire-Wrapped-Screen-104559/">wire wrapped screen</a></strong> US government data found that more than <strong><a href="http://www.hengyingwiremesh.com/p-Wire-Mesh-104571/Chain-Link-Fences-104590/">chain link fences </a></strong> one in four fathers - 27 per cent - with children 18 or younger live away from <strong><a href="http://www.hengyingwiremesh.com/p-Wire-Mesh-104571/Crimped-Wire-Mesh-104584/">Crimped Wire Mesh</a></strong>at least one of their children. <strong><a href="http://www.aoruiplasticmachine.com/">pipe production line</a></strong>That number is more than double the share of fathers <strong><a href="http://www.aoruiplasticmachine.com/p-pipe-production-line-104953/pvc-pipe-production-line-105351/">pvc pipe production line</a></strong>who lived apart from their children in 1960 <strong><a href="http://www.aoruiplasticmachine.com/p-pipe-production-line-104953/PE-pipe-production-line-105349/">pe pipe production line</a></strong>.
But married fathers who <strong><a href="http://www.aoruiplasticmachine.com/p-pipe-production-line-104953/PPR-pipe-production-line-105348/">ppr pipe production line</a></strong> live with their children are actually devoting more time <strong><a href="http://www.aoruiplasticmachine.com/p-sheet-extrusion-line-104525/plastic-sheet-extrusion-line-105337/">plastic sheet extrusion line</a></strong> helping their wives with caregiving at home - <strong><a href="http://www.aoruiplasticmachine.com/p-sheet-extrusion-line-104525/pe-foam-sheet-extruder-105334/">pe foam sheet extruder</a></strong>a task once seen almost exclusively as a woman's duty. <strong><a href="http://www.aoruiplasticmachine.com/p-plastic-extruder-105358/twin-screw-extruder-105361/">twin screw extruder</a></strong> Such fathers on average now spend <strong><a href="http://www.powersky-lightings.com/">cree LED Flashlight</a></strong>about 6.5 hours a week on child care, <strong><a href="http://www.powersky-lightings.com/p-LED-Flashlight-106052/Aluminum-LED-Flashlight-106057/">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="http://www.powersky-lightings.com/p-Bright-Flashlight-106081/">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="http://www.powersky-lightings.com/p-Bright-Flashlight-106081/Dynamo-Flashlight-106083/">dynamo flashlight</a></strong> 65 percent of those who didn't finish high school.
Age, too, was a factor. <strong><a href="http://www.powersky-lightings.com/p-Bright-Flashlight-106081/Keychain-Light-106403/">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="hhttp://www.powersky-lightings.com/p-LED-Headlamp-104393/CREE-LED-Headlamp-106405/">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="http://www.powersky-lightings.com/p-working-lights-104394/LED-Work-Light-106408/">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="http://www.powersky-lightings.com/p-Camping-Lantern-104398/LED-Camping-Lantern-106409/">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="http://serverfault.com/questions/412371/prevent-redirecting-for-specific-request-uri">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