views:

276

answers:

2

Simple question. Is there a limit as to how many RewriteRules I can have in my .htaccess or can I put a zillion of them in there without Apache going all funky on me?

Is there a relative limit where the server simply dives because there are to many rules to iterate through?

Thanks!

+1  A: 

If your RewriteRules include several is existing file (-F) or is existing url (-U) flags, since those are subrequests, you could see a performance hit. Outside of those, I haven't experienced a situation where several RewriteRules start adversely effecting performance and my current implementation has a good amount.

You can limit the need to iterate through all the RewriteRules by just ordering them in such a way that the more-expensive checks are done later on in the process (if possible), that way earlier conditionals can short circuit out of your rewrite logic and save you the computation of the more expensive rules later on in the process.

Kyle Walsh
+2  A: 

You have to know that the .htaccess configuration files are being processed on every request.

So if you have a .htaccess file with 1000 rules, the worst case is that every 1000 rules are tested every time a request hits this directory.

Therefore you should use a structure where a rule matches a request as early as possible. Rules that handle more frequent requests should appear before those that are less frequent and determine the processing (see L flag). Read about the ruleset processing to know how the rules are being processed (see also RewriteLogLevel direcitve).

Another factor are the regular expressions: Better use “simple” and efficient regular expressions than ambiguous or complex ones. You should look into the how regular expressions are interpreted and processed to avoid costly ones and get the most out of them.

Gumbo