Move your .htaccess directives to httpd.conf
Last week I was searching the net for a program or application to move some of our .htaccess directives into Apache’s configuration files. The reason I was looking for this is to improve the performance of the site. When we have directives in the .htaccess file, the performance hit on Apache on when it serves each URL is accessed.
If you have a .htaccess file in your web folders, Apache will have to parse the directives in this file and see if there are any .htaccess files in the parent folder. All this happens before your html or php file is hit by Apache. You can read more about the overheads of .htaccess in this article. This article .htaccess vs httpd.conf by Dawid Golunski shows that he saw apache served about 6.6% less requests/second when .htaccess was used.
A quick Google search landed me on Paul Reinheimer’s blog where he’s created a php script just for this purpose. His htaccess to httpd.conf script parses all the htaccess files from the current folder and all sub-folders and creates a configuration file with the directives for you to place in your Apache’s configuration file.
Just download his script from this link, and place it in your web-root directory which you want to generate the Apache configuration file and run the following command from the command prompt:
php htaccess.php >~/site.conf |
After you run that command, you should have a file site.conf in your home folder. You can open this file to get all the directives for you to place in the Apache configuration file. Once you’ve placed this in the apache configuration file, you’ll need to restart Apache for the changes to take effect. Before you restart, make sure you remove your .htaccess file from your web folders.
When I converted a Wordpress’ .htaccess file using this script, here’s the output I got
<directory var="" www="" blog="">
<ifmodule mod_rewrite.c="">
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</ifmodule>
</directory> |
I just put this into the apache2.conf and restarted it, removed the .htaccess from the webroot in /var/www/blog and all the rules worked as they should. As with any script, if you have more complex directives in your .htaccess, test your sites thoroughly before pushing your changes into the production server.
The only (minor) disadvantage I found with this method is that the apache server has to be restarted to make changes to your rule-sets you put in the configuration files. It seems apache just has to be reloaded and doesn’t have to be completely restarted to get the changes to the configuration to take effect (thanks Keleo for the tip). Also this conversion will not be possible if your site is running on a shared webhost where you don’t have access to the apache configuration files.
Links:
Paul Reinheimer’s Article .htaccess to httpd.conf
Paul’s htaccess.php Script
htaccess vs httpdconf – Benchmarks

![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=3f05f03d-69f9-41e2-af4f-bd4e21b4c515)
