In the past week I've dealt with some servers that were seeing some rather heavy load, with more traffic on the way. I noticed that the servers had AllowOverride All in their httpd.conf files, which immediately struck me as a problem. While .htaccess files can be quite handy during development (AllowOverride instructs Apache to process those .htaccess files), they're also quite horrible in production due to the performance hits involved in using them. Since the files allow configuration to be changed on the fly, they need to be read on every request. This requires a lot of unnecessary stat calls with each and every request.

I approached the development team requesting permission to turn the option off, in order to squeeze some extra performance out of each server. Rather than the happy smiles and agreement I expected, I was instead met with nervousness and uncertainty. Since the front end team uses them constantly, there was concerns that the change over from .htaccess to httpd.conf would consume a lot of time, and would be very difficult to maintain moving forward.

In order to allay their fears I created a .htaccess to httpd.conf script with the help of my friend Rich Bowen. It's designed to be executed from the command line from a sufficiently permissioned user from within the document root. It recursively scans all sub directories looking for .htaccess files, reads them in, and spits it all back out. There's a bit of intelligence involved in order to present the contents of .htaccess files with the least depth first (this way overriding works correctly), and warnings are generated if the RewiteBase instruction is encountered as it may require extra tweaking. Other than that I generally just pipe the output to htaccess.conf, then include that file from my basic configuration file. By re-running this script (and, of course, gracefully restating the server after checking to make sure all the conf files parse) as part of my normal sync process I get all the flexibility of .htaccess files, without the performance hit.

The script requires PHP 5, probably 5.1+ would be safest.


No warranties, test extensively. In terms of field testing (against my rather limited set of .htaccess files) it's been running on a site +10M uniques/day, using roughly 40 .htaccess files. I'd love to hear from you if you end up using it.

Source:.htaccess to httpd.conf converter, and again with thanks to Rich Bowen.

Comments »

Weblog: haydenjames.io
Tracked: Nov 20, 22:57
This is awesome, thanks for the post. I've secretly been copying .htaccess from svn and adding to my vhost confs, letting the developers think they have access to .htaccess. I'm surprised as to how few developers see or care about the adverse effect of using .htaccess in their production vhosts.
Can't wait to try this script out.
#1 Vid Luther (Homepage) on 2009-10-12 15:34 (Reply)

In the script should RedirectBase be RewriteBase?
#2 Mikko Koppanen (Homepage) on 2009-10-12 16:51 (Reply)

Could you please describe how well this change affected the load? Thanks
#3 Rob on 2009-10-12 19:31 (Reply)

This is pretty cool... I can't wait to try it out!
#4 zaemis (Homepage) on 2009-10-12 20:00 (Reply)

Well, it can only do good things, how good those things are depends on a lot of stuff.

- The more heavily loaded your server is, the more this will help.
- IO bound systems will see more than others
- The more nested your directory structure for file and URLs, the more .htaccess files apache will look for, so the more it will help.

So no hard and fast numbers, your mileage will vary.
#5 Paul Reinheimer (Homepage) on 2009-10-13 02:02 (Reply)

What a great script to share, I am sure this will come in very useful.

Do you have any kind of information about what kind of improvement you saw after making this change?
#6 Louis W on 2009-10-13 13:25 (Reply)

Great information. Thank you Paul.

Also some benchmarks would be so interesting :-)
#7 Stelian Mocanita (Homepage) on 2009-10-15 11:26 (Reply)

Please correct me if I am wrong but this bit of code(line:23-28) serves no purpose that I can notice.

[code]
//Merge base set of filters with any from the command line
if(count($argv) > 0)
{
unset($argv[0]);
$filters = array_merge($filters, $argv);
}
[/code]

It will only kick out a PHP Notice of an undefined variable. Unless this is a part of PHP I am not familiar with I am assuming the code above is useless.
#8 Jeremy Simkins on 2009-10-27 13:35 (Reply)

Please ignore my previous post, I didn't read the comments and now I just look stupid.

Thanks for the script! Very useful.
#9 Jeremy Simkins on 2009-10-27 13:37 (Reply)

Hi all,

I am a newbie in php
I want to know how to change the url address for remote access.
I don't know how to use .htaccess and httpd.conf

I found many topics related these but still can't.

e.g http://192.100.0.23/myfolder/login.php

to

http://192.100.0.23/newname

waiting for reply.....
#10 yoko on 2009-11-10 06:45 (Reply)


Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
 

Hi, I’m Paul Reinheimer, a developer working on the web.

I co-founded WonderProxy which provides access to over 200 proxies around the world to enable testing of geoip sensitive applications. We've since expanded to offer more granular tooling through Where's it Up

My hobbies are cycling, photography, travel, and engaging Allison Moore in intelligent discourse. I frequently write about PHP and other related technologies.

Search