Does .htaccess slow a site down?
It is incorrect to say that .htaccess will always slow down a site, because that is often based on the combination of .htaccess files along other factors like how much traffic your site receives. But, .htaccess files can slow down a site. Let’s go into more detail about why there is potential for a reduction in performance with .htaccess, and what you could do to avoid it.
How .htaccess can slow a site down
Assuming that your Apache web server has .htaccess support turned on, then every time a user requests a file on your web server each and every directory will be searched for an .htaccess file leading up to the actual requested file.
So, for example, let’s say someone requests the fictitious file called “https://www.programmerinterview.com/java/interview-questions/somepage.php”. This means that if .htaccess support is turned on in the server running programmerinterview.com, Apache would first look in the web root directory on the server, then the java directory, and finally the interview-questions directory to see if there are any .htaccess files in those directories.
Let’s assume that the web root directory of the Apache server resides at /home/username/public_html. Then, Apache would search for .htaccess files in these directories, in the order listed below:
/home/username/public_html/.htaccess /home/username/public_html/java/.htaccess /home/username/public_html/java/interview-questions/.htaccess
Having .htaccess enabled means every directory will be searched
Because these directories will be searched each and every time someone requests the “https://www.programmerinterview.com/java/interview-questions/somepage.php” file (or any other file in the interview-questions directory for that matter), this can become a performance hit.
Imagine a website that gets thousands of hits every few minutes – this will clearly slow down the site, and is something that should be avoided. But, for a site that gets far less traffic (like hundreds or thousands of visitors in an entire day), using an .htaccess file probably won’t be an issue at all. If you are a site owner and are concerned about the performance hit from having .htaccess files enabled, the best thing to do is test your site out with .htaccess files enabled and disabled and see if there are any differences in the performance.
What’s the alternative to using .htaccess?
The main alternative (as suggested by the Apache documentation itself) is to use a Directory section/directive inside the main server config file (usually named “httpd.conf”). This is actually a better option as opposed to using an .htaccess file, but many people don’t have access to that file since they are on shared hosting, so .htaccess files are offered instead.
Any and every directive placed inside an .htaccess file can also be made using a Directory directive inside the server configuration file. An.htaccess is meant to be used only when you do not have access to the server configuration file, which is often the case for people who are on shared hosting services. VPS users usually have access to the server configuration file.
A bigger performance hit for RewriteRule directives in .htaccess
The RewriteRule directive uses regular expressions. If your .htaccess file uses the RewriteRule directive, then it means that with each and every request to a file that resides in the same directory as the .htaccess file, those regular expressions will have to be recompiled. This can obviously be a big performance hit. But, if you actually move the RewriteRule directives to the main Apache configuration file, these rewrite rules will be compiled just once by the server – as opposed to compiling them each and every time a new request is made to the server. The reason the RewriteRules inside the main config file will only need to be compiled once by the server is because the main Apache config file is cached. However, .htaccess files are never cached by the Apache server.