views:

805

answers:

3

I know there are a lot of positive things mod-rewrite accomplishes. But are there any negative? Obviously if you have poorly written rules your going to have problems. But what if you have a high volume site and your constantly using mod-rewrite, is it going to have a significant impact on performance? I did a quick search for some benchmarks on google and didn't find much.

+10  A: 

I've used mod_rewrite on sites that get millions/hits/month without any significant performance issues. You do have to know which rewrites get applied first depending on your rules.

Using mod_rewrite is most likely faster than parsing the URL with your current language.

If you are really worried about performance, don't use htaccess files, those are slow. Put all your rewrite rules in your Apache config, which is only read once on startup. htaccess files get re-parsed on every request, along with every htaccess file in parent folders.

Ryan Doherty
Good answer, but I should point out that *enabling* .htaccess files is the issue, not so much using them. If you don't disable them entirely, you still have the stat-hit for every path segment even if you don't have a single .htaccess file anywhere.
Michael Cramer
+3  A: 

If you're worried about apache's performance, one thing to consider if you have a lot of rewrite rules is to use the "skip" flag. It is a way to skip matching on rules. So, whatever overhead would have been spent on matching is saved.

Be careful though, I was on a project which utilized the "skip" flag a lot, and it made maintenance painful, since it depends on the order in which things are written in the file.

bpapa
It may be better to use the [L] flag (indicating that *all* following rules should be skipped if the current one matches) in some circumstances, no?
Frank Farmer
+3  A: 

To echo what Ryan says above, rules in a htaccess can really hurt your load times on a busy site in comparison to having the rules in your config file. We initially tried this (~60million pages/month) but didn't last very long until our servers started smoking :)

The obvious downside to having the rules in your config is you have to reload the config whenever you modify your rules.

The last flag ("L") is useful for speeding up execution of your rules, once your more frequently-accessed rules are towards the top and assessed first. It can make maintenance much trickier if you've a long set of rules though - I wasted a couple of very frustrating hours one morning as I was editing mid-way down my list of rules and had one up the top that was trapping more than intended!

We had difficulty finding relevant benchmarks also, and ended up working out our own internal suite of tests. Once we got our rules sorted out, properly ordered and into our Apache conf, we didn't find much of a negative performance impact.

ConroyP