2

Does the number of directives in a .htaccess file impacts performance and page loading time in a significant way? If so, how much directives or line would be acceptable?

Say, for example, I want to use HTML5 Boilerplate .htaccess plus the directives suggested in the items 8 and 9 of this link, and all directives suggeste in this one in a WordPress multisite install (around 900 lines).

I know that is better -- performance wise -- to use httpd.conf than .htaccess files. But In a shared hosting / managed hosting environment, where I can't access apache config, I wonder what the best practice is.

Celso Bessa
  • 145
  • 7
  • 1
    IMHO you must think if you really need so many lines of code. Never copy paste code you do not understand – Max Muster Nov 23 '17 at 23:36

1 Answers1

2

The number of directives definitely impacts webserver performance and thus page load time. As you noted, performance-wise, placing such directives in the httpd.conf is preferred since those directives are parsed one time while .htaccess directives are processed for every request to that directory or a child item within a subdirectory.

The performance difference is usually negligible (milliseconds) for many small websites. However, if your site has a lot of traffic, or for that matter, is on a shared webserver with several other sites which aggregate to heavy usage/volume, then the performance degradation can definitely be more observable.

There's no defined "recommended number" of directives, but a good rule of thumb for a shared hosting environment is to only add the directives you absolutely need to .htaccess. Your hosting provider's cpanel may often include webserver application configuration options at the httpd.conf level for things like:

  • Type Handlers
  • Cache Expiration
  • Permissions (directory listings)
  • Compression

This post also discusses more about performance considerations: https://stackoverflow.com/questions/25063677/how-much-does-using-htaccess-files-slow-down-website-performance-especially-wit

Anson W Han
  • 394
  • 1
  • 6
  • Thanks for the comprehensive answer, [Anson](https://serverfault.com/users/439527/anson-w-han) – Celso Bessa Nov 24 '17 at 14:12
  • and not only performance reasons, spreaded files, per-dir configurations are much more complex in most cases than those which can just be placed under virtualhost context. The recommendation is to never use them if you can avoid it. – ezra-s Nov 24 '17 at 15:15