as i have discover the 'sometimes not-apparent way' that mod_rewrite handles the different rewrite rules you feed it (eg: first reading through RewriteRule, then returning to check on RewriteCond), but:

so far as performance can be involved, does an order from the rules matter?

does apache process these questions 'top down' way? and therefore the most famous rules should technically be as near to the top list as you possibly can?

this isn't an issue regarding overlapping rules etc, with this question think that all of the rules are [L] and non-overlapping.


should the most famous rules be as near to the top as you possibly can, as the less used ones close to the bottom?


While all what @Corey Henderson stated makes absolutely sense .. it's not 100% matches the truth.

Only have these 2 rules inside your .htaccess (I understand, this really is a little stupid example, however, you might run in to the same effect eventually when you are performing complex rewrites):

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

What you know already -- redirect ALL demands to index.php. Flag [L] is placed so absolutely nothing to worry. Well- apparently it's something to bother with, as having seen [L] flag mod_rewrite goes to another iteration (getting into a loop). Because we now have rule which will always performed, we'll have endless loop (well, there's establishing Apache config that controls it -- automatically it's maximum 10 iterations). If limit is exceeded then you'll see 500 Server Error message which line in Apache's error.log: "Request exceeded the limit of 10 internal redirects because of probable configuration error. Use 'LimitInternalRecursion' to improve the limit if required. Use 'LogLevel debug' to obtain a backtrace."

The rewrite stop if:

  1. Forget about rules to process
  2. Exterior Redirect [R=301]
  3. Explicit "absolutely nothing to rewrite" command is offered (second parameter of RewriteRule -- destination ought to be -.
  4. When spinning to the identical URL as on beginning of iteration.
  5. Already pointed out "Request exceeded the limit of xx internal redirects".

So yeah .. the faster rewrite iteration is going to be interrupted (rule is on top) the greater it's.

When ordering your rules (if you have a number of of these, not only 1-2-3) you might think about this logic (which rules continues top):

  1. Rules for files/folders that you simply don't want to the touch Whatsoever, under any conditions (process request out of the box, no matter domain/protocol)
  2. Rules which do change domain (redirecting to www. for instance) or protocol (forcing HTTPS) -- the faster you need to do this the greater it's (just like you get it done past too far it might already change URL from "nice" to real).
  3. Other important rules that could affect existing files/folders
  4. (Consider getting this) "absolutely nothing to rewrite" for existing files/folders (see below, type of #1 however for all existing assets)
  5. Other rules
  6. Catch all rule.

On huge most of sites where spinning Web addresses is within place you won't convey more that 5-6 rules (default .htaccess files for many PHP frameworks I've come across + some items like WordPress have just "catch all" (if file/folder doesn't exist then rewrite request to index.php)).

Every website may have their very own logic making the above mentioned list only a general recommendation, anything.

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

My primary point here's: For those who have a number of rules, consider placing this type of "absolutely nothing to rewrite" somewhere there to prevent the iteration completely.

In the documentation for RewriteRule:

An order by which these rules are defined is essential - this is actually the order by which they'll be applied at run-time.

If you put your common rewrite rules at the very top, and whenever you can mark all of them with the L flag, it will not process the relaxation from the rules, you'll also find an enhanced configuration:

lastL - Stop the spinning process immediately and do not apply anymore rules