Rewrite rule performance impact when using apache mod_rewrite

while I read about the "sometimes unobvious way" that mod_rewrite handles the various rewrite rules that you feed it (for example: first reading the RewriteRule and then returning to the RewriteCond check), but:

Regarding performance, does the order of the rules matter?

Does apache process them from top to bottom? mean that the most frequently used rules should be technically as close as possible to the top of the list?

it is not a question of overlapping rules, etc., for this question it is assumed that all rules are [L] and non-overlapping.

So,

should the most used rules be as close to the top as possible and the less used rules below?

thank!

+5
source share
2 answers

From the documentation for RewriteRule:

http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

The order in which these rules are defined is important - this is the order in which they will be applied at runtime.

So, if you put your general rewrite rules at the top and, if possible, mark them with the L flag , it will not process the rest of the rules, and you will have an optimized configuration:

last | L - Stop the rewriting process immediately and do not apply more rules

+4
source

While all that @Corey Henderson said makes absolutely perfect sense. This does not correspond to 100% reality.

2 .htaccess( , , ):

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

- index.php. [L] , . - , , [L] mod_rewrite ( ). , , (, Apache - 10 ). , 500 Server Error Apache error.log: " 10 - . " LimitInternalRecursion ", , . " LogLevel " debug ', .

, :

  • [R=301]
  • " " ( RewriteRule - -.
  • URL, .
  • " xx".

, .. ( ), .

( , 1-2-3), ( ):

  • /, , ( , /)
  • , (, www.) ( HTTPS) - , ( URL "nice" ).
  • , /
  • ( ) " " / (. , # 1, )
  • .

, URL-, 5-6 ( .htaccess PHP, + , WordPress, " " ( /folder , index.php)).

, , .


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

: , , " " - , .

+7

All Articles