Forum Moderators: phranque

Message Too Old, No Replies

Number of RewriteRules in .htaccess question

         

chy123

10:27 am on Jul 2, 2009 (gmt 0)

10+ Year Member



Just wondering, if I have 50 RewriteRules in my .htaccess file and someone went to the website and only the 49th rule in the list would apply to the url entered, does apache still need to check whether the 48 rules before that rule are needed ? Does apache cache rules ?

Anyone noticing performance issues after having too many rewriterules ?

jdMorgan

2:51 pm on Jul 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



50 rules? That's not a lot...

How many is too many depends on your server capability (CPU speed, memory speed and size, disk speed, etc.), the backbone connection speed, and the number and time-distribution of client requests to your server. As a result, *you* will notice performance problems when and as your site gets busy.

mod_rewrite will process the pattern-matches in all RewriteRules in your .htaccess files, and process the RewriteConds only for the rules whose patterns match. If both the rule and cond patterns match, the rule will be invoked, and if it's an internal rewrite and the rule has an [L] flag on it, then mod_rewrite processing will be re-started from the top. After a complete pass through all of the rules is made and none match, mod_rewrite processing will end. If your rules do not have [L] flags on them, then all rules will be repeatedly processed and invoked until no further rules match.

I wouldn't worry about 50 rules, though. On some projects, I'd call adding 50 rules "a good start to the morning's work." :)

That said, I'm also a firm believer in making those rules' patterns as specific as as possible, and using the most efficient coding techniques possible. So it's possible that your 50 rules may be slower than my 400 rules, simply because my regex patterns are better, for example.

The top killers of performance I usually see are:

  1. Unnecessary file- and directory-exists checks (RewriteCond with -d and -f, for example -- as with standard WordPress and Joomla installations).
  2. Unnecessary reverse-DNS lookups.
  3. Multiple ".*" subpatterns in regex patterns - for example, "^(.*)/(.*)/(.*)\.html$".
  4. Missing [L] flags, especially on external redirect rules.

Jim