The Fubra Blog
.htaccess vs httpd.conf
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.10 | 4539 | |
| 1.13 | 4429 | |
| 1.12 | 4451 | |
| 1.13 | 4424 | |
| 1.67 | 4683 | |
| Average | 1.23 | 4505 |
| .htaccess enabled: | ||
| Time (s) | Requests per second | |
| 1.85 | 4606 | |
| 1.21 | 4121 | |
| 1.19 | 4188 | |
| 1.22 | 4098 | |
| 1.24 | 4026 | |
| Average | 1.34 | 4208 |
That gives us a difference of around 6.6% less requests per second while .htaccess is turned on.
Tags: .htaccess, Apache, Configuration
You have forgotten to tell Apache version which you tested.
While this is an interesting test, its only part of the story because the .htaccess files were bereft of any rules or directives.
What you have done is successfully benchmarked your Apache instances ability to seek and open an .htaccess file on your setup.
Fairly useless information to anybody else. Repeating the test was a good idea, but i would like to have seen a longer test and the .htaccess file contain a directive even if it was benign.