apache display:disable .htaccess

A while back, I searched the web for a demonstration article that I could forward to a client. stacklinux, An article explains how to use wordpress cache plugins that use .htaccessAlmost always slower than plugins that don’t use .htaccess.

Surprisingly, that quest took a lot longer than expected, until I finally came to this article, Martin, from foliovisionraised a good point in the comments section of that post when he said: “I wonder if WP Super Cache is as efficient in PHP mode [as .htaccess mode] , That scenario brings us to the very point of this blog post.

Why should you disable .htaccess?

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

Really for two reasons, performance and security. For the scope of this article we will focus on the unnecessary performance overhead of using .htaccess. Now this is not a new or hidden tweak but it is officially documented In form of recommended best practices, I’ve updated this article because I still notice frequent use of Apache’s .htaccess by web server administrators.

Here’s an excerpt from the Apache docs:

In general, you should only use .htaccess files when you do not have access to the main server configuration file. For example, there is a common misconception that user authentication should always be done .htaccess files, and, in recent years, another misconception that mod_rewrite should go to the instructions .htaccess files. This is simply not the case. You can keep the user authentication configuration in the main server configuration, and in fact, this is the preferred way of doing things. likewise, mod_rewrite In the main server configuration, directives work better in many ways.

.htaccess Files should be used in a case where 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 unwilling to make frequent configuration changes, it may be desirable to allow individual users to make these changes. .htaccess files for myself. This is especially true, for example, in cases where ISPs are hosting multiple user sites on the same machine, and want their users to be able to change their configuration.

However, in general, use .htaccess Files should be avoided when possible. Any configuration you want in a . will consider putting in .htaccess file can be created as effectively as a <Directory> section in your main server configuration file.

There are two main reasons to avoid using .htaccess files.

The first of these is performance. When AllowOverride ready to allow the use of .htaccess files, httpd will look in each directory for it .htaccess files. Thus, allowing .htaccessFiles cause a performance hit, whether you even actually use them or not! This also .htaccess The file is loaded every time a document is requested.

further note that httpd must see .htaccess files in all top-level directories, to be the full complement of instructions to which it must apply. (see section How do the instructions apply?.) Thus, if a file is requested from a directory /www/htdocs/examplehttpd should see the following files:


And so, to access each file from that directory, there are 4 additional file-system accesses, even if none of those files exist. (note that this will only happen if.htaccess files enabled /Which is usually not the case.)

in the matter of RewriteRule instructions, in .htaccess In the context these regular expressions have to be recompiled with each request to the directory, whereas in the main server configuration context they are compiled once and cached. Additionally, the rules themselves are more complex, as must work around the restrictions that come with per-directory references and mod_rewrite,

Takeaway from Apache Docs

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

You should only use .htaccess if you are on a shared hosting plan, do not have root access to the webserver, or if you do not have experience modifying Apache’s configuration files. Using .htaccess files affects performance, regardless of the content of your .htaccess file.

In fact, it slows down Apache even if your .htaccess is empty Since it has been reloaded everyone Request! Thus, the contents of .htaccess must be imported into your Apache server configuration file where it is compiled Once, After which you should stop using .htaccess completely.

Disabling .htaccess and using mod_rewrite within Apache config

Use of .htaccess files can be disabled completely Allow Override instructions to Nobody,

AllowOverride None

Here’s an example of an Apache configuration:

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

Options -Indexes FollowSymLinks

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

#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]
# END WordPress

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

Interested in further enhancing the performance of Apache? Related Reading: Strip Down Apache and Recommend WordPress Caching Plugins to Improve Performance and Memory Efficiency)

Originally Published: November 8, 2016

Leave a Comment