Apache Performance: Disable .htaccess

In 2013, while seeking a detailed guide on performance enhancements for a StackLinux client, I began a quest to find an article that explained the impact of WordPress Caching plugins—specifically, how those utilizing .htaccess might be less efficient than their counterparts.

This search turned out to be unexpectedly time-consuming, but it eventually led me to this article. In the comments section, Martin from Foliovision posed a thought-provoking question: “I wonder if WP Super Cache in the PHP mode is just as efficient [as .htaccess mode]?” This inquiry is the cornerstone of the discussion we’re about to delve into.

Note: This article, first published on November 8th, 2016, and most recently updated on February 27th, 2024, has been revisited once more.

The reason for its latest update is the persistent and widespread use of Apache’s .htaccess among web hosts and server administrators. Interestingly, it’s only in recent times that a select few web hosting services have started to discontinue support for .htaccess or have begun advising against its usage.

As of February 2024, Apache is used by 30% of all websites. This includes 22% of websites of the top 1,000,000 websites.

Why you SHOULD disable .htaccess

Disable .htaccess
Source: httpd.apache.org/docs/2.4/howto/htaccess.html#when 

You should disable .htaccess, for two reasons really, performance and security. For the scope of this article, we will focus on the unnecessary performance overhead of using .htaccess.

This is not some new or hidden tweak, but it’s officially documented as the recommended best practice.

Here’s an excerpt from Apache docs:

In general, you should only use .htaccess files when you don’t have access to the main server configuration file. There is, for example, a common misconception that user authentication should always be done in .htaccess files, and, in more recent years, another misconception that mod_rewrite directives must go in .htaccess files. This is simply not the case. You can put user authentication configurations in the main server configuration, and this is, in fact, the preferred way to do things. Likewise, mod_rewrite directives work better, in many respects, in the main server configuration.

.htaccess files should be used in a case where the content providers need to make configuration changes to the server on a per-directory basis, but do not have root access on the server system. In the event that the server administrator is not willing to make frequent configuration changes, it might be desirable to permit individual users to make these changes in .htaccess files for themselves. This is particularly true, for example, in cases where ISPs are hosting multiple user sites on a single machine, and want their users to be able to alter their configuration.

However, in general, use of .htaccess files should be avoided when possible. Any configuration that you would consider putting in a .htaccess file, can just as effectively be made in a <Directory> section in your main server configuration file.

There are two main reasons to avoid the use of .htaccess files.

The first of these is performance. When AllowOverride is set to allow the use of .htaccess files, httpd will look in every directory for .htaccess files. Thus, permitting .htaccessfiles causes a performance hit, whether or not you actually even use them! Also, the .htaccess file is loaded every time a document is requested.

Further note that httpd must look for .htaccess files in all higher-level directories, in order to have a full complement of directives that it must apply. (See section on how directives are applied.) Thus, if a file is requested out of a directory /www/htdocs/example, httpd must look for the following files:

/.htaccess
/www/.htaccess
/www/htdocs/.htaccess
/www/htdocs/example/.htaccess

And so, for each file access out of that directory, there are 4 additional file-system accesses, even if none of those files are present. (Note that this would only be the case if.htaccess files were enabled for /, which is not usually the case.)

In the case of RewriteRule directives, in .htaccess context these regular expressions must be re-compiled with every request to the directory, whereas in main server configuration context they are compiled once and cached. Additionally, the rules themselves are more complicated, as one must work around the restrictions that come with per-directory context and mod_rewrite.

The takeaway from Apache Docs

Well, it’s clear to see that the .htaccess feature is just another example of just how flexible the Apache server is. Unfortunately, .htaccess is still being used when it should be avoided.

The use of .htaccess should be limited to specific scenarios: if you’re on a shared hosting plan, lack root access to the web server, or are not confident in modifying Apache’s configuration files. It’s important to note that .htaccess files inherently decrease performance, regardless of their content.

This performance reduction occurs because Apache reloads the .htaccess file with every request, even if the file is empty. Therefore, server administrators are encouraged to transfer .htaccess directives into Apache’s main server configuration file, which is only compiled once. Following this, disabling .htaccess entirely is advisable to optimize performance.

Disabling .htaccess and using mod_rewrite within Apache config

You can disable the use of .htaccess files completely by setting the AllowOverride directive to none:

AllowOverride None

Here’s an example of Apache config:

<VirtualHost *:80> 
ServerName linuxblog.io
ServerAlias linuxblog.io www.linuxblog.io
DocumentRoot /var/www/root/

Options -Indexes FollowSymLinks

#disable htaccess starting at web root 
<Directory /> 
AllowOverride none 
</Directory>

#Import contents of wordpress .htaccess
<Directory /var/www/root/> 
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
</Directory> 
</VirtualHost>

If you are using WP Super Cache with mod_rewrite or even W3 Total Cache, you can cut and paste the entire contents of .htaccess into Apache’s config just like the above example for performance gains. Remember to disable .htaccess completely, or Apache will still search for it on every request.

Are you interested in increasing Apache’s performance further? Related reading: Strip Down Apache to Improve Performance & Memory Efficiency and recommended WordPress Caching Plugins.

Also, read Nginx tuning tips: TLS/SSL HTTPS – Improved TTFB/latency.

Conclusion

For those managing websites and seeking optimized server performance, the guidance is clear: disable .htaccess when possible by integrating necessary directives directly into Apache’s main server configuration.

This approach not only enhances server response times but also strengthens security measures. As we’ve seen, moving away from .htaccess to direct Apache configuration can offer tangible performance improvements, making it a crucial consideration for web administrators aiming to maximize their site’s speed and reliability.

In an era where website performance can greatly influence user experience and SEO rankings, such optimizations are more than just technical adjustments—they’re essential strategies for ensuring the digital competitiveness of your online presence.

Tags: , , ,

Discussion

  1. .htaccess is useful to those on shared hosting where they can’t use Apache for the directives.

    You disable it to get every last bit of performance out of Apache which makes sense to me!



Top ↑