11

Are htaccess redirects any slower/put any more strain on Apache than redirects in configuration files? Also, how big can a number of redirects in htaccess get before they start decreasing performance?

Joe Lencioni
  • 10,231
  • 18
  • 55
  • 66
deadprogrammer
  • 11,253
  • 24
  • 74
  • 85

3 Answers3

16

Yes, it slows the server because it has to access the file each time a resource in that directory or any subdirectory thereof is accessed.

The amount of redirects is not relevant, because the main performance hit is the file access itself. This within reasonable constraints (ie a 5 Kb htaccess file will take more or less the same time to be parsed than a 1 Kb one, different story is a 1Mb htaccess, though I've never seen that monstrosity and hope I never will)

Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
9

While it's true that the .htaccess is parsed on each request, and is thus technically slower than putting your rules in the main config file, in reality it doesn't matter. The apache configuration engine is fairly optmized C code that's embedded in the web server. Unless you're only serving small static files with no database accesses at all, the extra overhead of .htaccess and redirects is negligible.

Modern processors are so fast that you'd really have to be doing a massive amount of traffic to worry about this. If you're doing this much traffic, and since it's all static content, go ahead and buy yourself a second server to share the load.

bmdhacks
  • 15,841
  • 8
  • 34
  • 55
  • So you'd just buy a second machine you could have saved yourself just by avoiding .htaccess files? It's not necessarily a single file access. – Vinko Vrsalovic Oct 16 '08 at 16:19
  • Even if it's multiple accesses, the amount of traffic you'd need for it to matter is massive. Go do one database hit and it stops mattering too. At this point, your bandwidth bill is so high, the cost of an extra server is negligible. Your millions of customers will appreciate the redundancy. – bmdhacks Oct 16 '08 at 16:27
  • 5
    Just for a little background, I was the maintainer of apache for mp3.com during the .com boom. We pushed nearly a gigabit of bandwidth and used mod_rewrite extensively on Pentium II computers from 1999. I've contributed code to apache that's in mod_rewrite. Redirects never were an issue. – bmdhacks Oct 16 '08 at 16:31
  • And mp3.com used .htaccess files? what for? – Vinko Vrsalovic Oct 16 '08 at 20:34
  • 5
    Look, after 10 years of hacking on apache's internals, and dealing with massive scalability, I'm just asking for the benefit of the doubt here. It kills me that the poor guy who asked this question is gonna run of and embark on some fruitless pursuit of unneeded optimization. – bmdhacks Oct 16 '08 at 21:21
  • Like avoiding htaccess files? Wow, now that's a hard, long and tough road ;). I agree that it's not a huge deal, but dismissing it does not answer the question, and there are cases in which it does matter. One database hit you may not be able to avoid, htaccess usage is mostly superfluous [...] – Vinko Vrsalovic Oct 16 '08 at 21:52
  • [...] except when doing public hosting. Any other use of htaccess files is just better placed in the main config file instead. It's not like you have to rewrite all your rules and waste months of work or anything like that. – Vinko Vrsalovic Oct 16 '08 at 21:56
1

Using a .htaccess file is slower than using a configuration file - a .htaccess file is parsed whenever a request is made to a directory it affects - this allows for changing the file without restarting the server. Since a configuration file is parsed only once at server start, it's faster.

The amount of directives you can have in a .htaccess file without significant performance impact will be based on the complexity of the rules and your server's specifics, although the main performance hit will be from using the .htaccess file at all.

Zxaos
  • 7,791
  • 12
  • 47
  • 61