Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

performance impact of order of rewrite rules when using apache mod_rewrite

while i have read about the 'sometimes un-obvious way' that mod_rewrite handles the various rewrite rules you feed it (eg: first reading RewriteRule, then going back to check RewriteCond), but:

as far as performance is concerned, does the order of the rules matter?

does apache process them in a 'top down' way? meaning that the most used rules should technically be as close to the top of the list as possible?

this is not a question regarding overlapping rules etc, for this question assume that all the rules are [L] and non-overlapping.

so,

should the most used rules be as close to the top as possible, while the less used ones near the bottom?

thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

While all what @Corey Henderson said makes absolutely perfect sense .. it is not 100% matches the reality.

Have only these 2 rules in your .htaccess (I know, this is a bit stupid example, but you may run into the same effect eventually when doing complex rewrites):

RewriteEngine On
RewriteRule (.*) /index.php?u=$1 [L]

You would think -- redirect ALL requests to index.php. Flag [L] is set so nothing to worry. Well -- apparently it is something to worry about, as after seeing [L] flag mod_rewrite goes to the next iteration (entering into a loop). Because we have rule that will always executed, we will have endless loop (well, there is setting in Apache config that controls it -- by default it is maximum 10 iterations). If limit is exceeded then you will see 500 Server Error message and this line in Apache's error.log: "Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace."

The rewrite will stop if:

  1. No more rules to process
  2. External Redirect [R=301]
  3. Explicit "nothing to rewrite" command is given (second parameter of RewriteRule -- destination should be -.
  4. When rewriting to exactly the same URL as on beginning of iteration.
  5. Already mentioned "Request exceeded the limit of xx internal redirects".

So yeah .. the faster rewrite iteration will be interrupted (rule is on the top) the better it is.

When ordering your rules (when you have quite a few of them, not just 1-2-3) you may consider this logic (which rules goes on top):

  1. Rules for files/folders that you do not want to touch AT ALL, under any circumstances (process request as is, regardless of domain/protocol)
  2. Rules that do change domain (redirecting to www. for example) or protocol (forcing HTTPS) -- the quicker you do this the better it is (as if you do it too late it may already change URL from "nice" to real).
  3. Other important rules that may affect existing files/folders
  4. (Consider having this) "nothing to rewrite" for existing files/folders (see below, kind of #1 but for all existing resources)
  5. Other rules
  6. Catch all rule.

On vast majority of sites where rewriting URLs is in place you will not have more that 5-6 rules (default .htaccess files for most PHP frameworks I have seen + some products like WordPress have just "catch all" (if file/folder does not exist then rewrite request to index.php)).

Every website will have their own logic which makes the above list just a general recommendation, nothing more.


# Do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

My main point here is: If you have quite a few rules, consider inserting this kind of "nothing to rewrite" somewhere there to stop the iteration completely.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...