Fubra Blog

Dawid Golunski

.htaccess vs httpd.conf

Posted 1:19 AM Monday January 7, 2008 by Dawid Golunski

If you are an Apache user you will have certainly heard about the two different ways to make configuration changes to Apache server. The first one is to make changes directly inside the main configuration file (i.e httpd.conf) and the second is to use .htaccess files. The second method allows us to make configuration changes on a per-directory basis.

There are some general opinions that tell you to never use .htaccess files on your web server, putting all the options in the main configuration file.

Here is a quote from Apache documentation:
When AllowOverride is set to allow the use of .htaccess files, Apache will look in every directory for .htaccess files. Thus, permitting .htaccess files causes a performance hit, whether or not you actually even use them! Also, the .htaccess file is loaded every time a document is requested.
which sounds pretty scary! Since .htaccess files offer many advantages including much greater flexibility (possibility of making changes on the fly without root access, and without a need to restart the server), we decided not to give up on using them that easily and to carry out some tests ourselves to see the actual impact they have on performance.

Setting up a test environment

First of all we need to edit our Apache httpd.conf file in order to set up two separate directories and enable .htaccess files for one of them:

<Directory /var/www/html/htaccess-enabled>
    Options FollowSymLinks
    AllowOverride All
</Directory>
<Directory /var/www/html/htaccess-disabled>
    AddHandler php5-script .php_script
    Options FollowSymLinks
    AllowOverride None
</Directory>

Now we need to create the directories:
# mkdir /var/www/html/htaccess-enabled
# mkdir /var/www/html/htaccess-disabled

and .htaccess file:
# echo 'AddHandler php5-script .php_script' \ 
>/var/www/html/htaccess-enabled/.htaccess

We also need to create simple pages so we could request them later:
# echo '.htaccess enabled' \
>/var/www/html/htaccess-enabled/enabled.html
# echo '.htaccess disabled' \
>/var/www/html/htaccess-disabled/disabled.html

After restarting the server:
# apachectl restart
we can start our tests.

Testing

By using the following command we can see how long it will take to request each of the pages 500 times:
# time for i in `seq 5000`; do \ 
curl http://localhost/htaccess-disabled/disabled.html >/dev/null;done
real    0m51.677s
user    0m28.482s
sys     0m21.101s

# time for i in `seq 5000`; do \
curl http://localhost/htaccess-enabled/enabled.html >/dev/null;done
real    0m52.217s
user    0m28.206s
sys     0m21.317s

In order to get more comprehensive results we can use ab (Apache Benchmark) program as follows:

# ab -n 5000 -c10 http://localhost/htaccess-disabled/disabled.html
# ab -n 5000 -c10 http://localhost/htaccess-enabled/enabled.html

-n denotes number of requests, and -c number of requests to be sent at the same time.
Repeating the test 5 times for each of the URL's we achieve the following results:

.htaccess disabled:
Time (s)Requests per second
1.104539
1.134429
1.124451
1.134424
1.674683
Average 1.234505

.htaccess enabled:
Time (s)Requests per second
1.854606
1.214121
1.194188
1.224098
1.244026
Average 1.344208

That gives us a difference of around 6.6% less requests per second while .htaccess is turned on.