views:

1048

answers:

4

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?

+1  A: 

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  A: 

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
+2  A: 

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
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
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
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
And mp3.com used .htaccess files? what for?
Vinko Vrsalovic
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
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
[...] 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
A: 

@bmdhacks - thanks for your entry. From what it sounds like if I have like 10000 page views a day on a shared server it's likely not going to matter (assuming the load isn't excessive with the other sites being hosted on the same server). I really don't want to find an alternative to the redirects unless I absolutely have to!

Wonkie