Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
litespeed_wiki:troubleshooting:rewrite_difference_on_l_flag_between_lsws_and_apache [2019/01/03 16:21]
Jackson Zhang created
litespeed_wiki:troubleshooting:rewrite_difference_on_l_flag_between_lsws_and_apache [2019/03/19 15:32] (current)
Lisa Clarke Fixed live example.com links
Line 1: Line 1:
-====== ​Minor difference on [L] flag in rewrite rules between LSWS and Apache ​======+====== ​The Rewrite Rule [L] Flag ====== 
 +There is a minor difference in the way LiteSpeed Web Server and Apache treat the ''​[L]''​ flag in rewrite rules. The ''​[L]''​ flag ("​L"​ stands for "​Last"​) causes mod_rewrite to stop processing the rule set. There are two ways to interpret this:
  
-The [L] flag (last flag) causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. LSWS does exactly like that and exit from the rewrite completely to avoid rules looping. ​However, Apache may run the ruleset again for the rewritten request(next iteration or integrations). ​In another world, for Apache, ​the [L] flag means "do not process any rules below in this iteration"(but not including future interactions)Therefore ruleset after [L] may still be processed on Apache, ​but not on LSWS.+  - In most contexts, including LiteSpeed Web Server, this means that if the rule matches, ​//no further rules will be processed//. LSWS exits from the rewrite completely to avoid looping. 
 +  - In Apache, ​''​[L]'' ​means that if the rule matches, no further ​rules will be processed //in the current ​iteration//. Apache ​does not exit from the rewrite rule completelyand so there is a possibility of looping.
  
-On Apache 2.4 and above, ​an alternative flag, [END], has been introduced to terminate ​not only the current round of rewrite processing but prevent any subsequent rewrite processing from occurring in .htaccess. [L] flag to LSWS is equivalent to [END] to Apache 2.4+**TL;DR: The ruleset after an [Lflag may still be processed by Apache on a subsequent iterationbut that is not true with LSWS and most other contexts.**
  
-How to make the Apache rules work with LiteSpeed? You may need to tweak your rule set to meet your requirement+In Apache 2.4 and above, an alternative flag, ''​[END]'',​ has been introduced ​to terminate not only the current round of rewrite processing, but to prevent any subsequent rewrite processing from occurring in ''​.htaccess''​. In other words, the ''​[L]''​ flag in LSWS is equivalent ​to ''​[END]''​ in Apache 2.4
  
-For exmaple:+===== Adjusting Apache Rules for LiteSpeed ===== 
 + 
 +You may need to tweak your Apache rule set to work with LiteSpeed. 
 + 
 +==== Tips ==== 
 +Changing the ruleset order may resolve the issue. 
 + 
 +Try using only one ''​[L]''​ in your ruleset instead of multiple ''​[L]''​s. 
 + 
 +Sometimes the situation may be more complicated. The user may have some rules inherited from an older Apache version, such as Apache 2.2, with a lot of ''​[L]''​s. It may not be easy to make it work for both Apache and LiteSpeed at the same time by simply changing the rule order or removing some ''​[L]''​s. You may try to make the ruleset work for LiteSpeed, but that may break it for Apache. In such cases, we would recommend leaving the Apache ruleset untouched, and enclose it with ''<​IfModule !litespeed>​ ... </​IfModule>''​. Then create a new ruleset for LSWS enclosed with ''<​IfModule litespeed>​ ... </​IfModule>''​ as a workable alternative. 
 + 
 +Writing rewrite rules may require a deep understanding of how the rewrite engine works and a talent for coding. The end user's rule may be complicated. They may have inherited a lengthy ruleset from a legacy Apache version, or maybe they are not following best practices, or perhaps they even involve some mistake. The final goal is to tweak the existing rules set to work with both LiteSpeed and Apache as quickly as possible, and not to worry about converting the ruleset into a piece of art.   
 + 
 +==== Example 1: Change the Order of Rules ==== 
 +Let's say you have this for Apache:
  
   RewriteEngine On   RewriteEngine On
Line 16: Line 32:
   RewriteRule ekipmanlar-(.*)- ekipmanlar.php?​urun=$1   RewriteRule ekipmanlar-(.*)- ekipmanlar.php?​urun=$1
  
-When the second line ''​RewriteRule ^([^\.]+)$ $1.php [NC,​L]''​ matched, LSWS will exit the rewrite and won't process any rules below while Apache may continue to process the rest rules at next iteration.  ​Change ​the rule order to the last line should ​resolve ​the problem.+When the second line ''​RewriteRule ^([^\.]+)$ $1.php [NC,​L]'' ​is matched, LSWS will exit the rewrite and won't process any rules belowwhile Apache may continue to process the rest of the rules at the next iteration.  ​Changing ​the rule order so the ''​[L]''​ is on the last line should ​cause both Apache and LSWS to treat this ruleset ​the same way.
  
   RewriteEngine On   RewriteEngine On
Line 25: Line 41:
   RewriteRule ^([^\.]+)$ $1.php [NC,​L] ​   ​   RewriteRule ^([^\.]+)$ $1.php [NC,​L] ​   ​
  
-Another tip is to use only one [L] in your ruleset instead ​of Multi [L]s.+==== Example 2: Use Only One [L] Instead ​of Multiple ====
  
-Sometimes the situation may be more complicated since the user may have some rules inherited from older apache version such as Apache 2.2 with a lot of [L]s and it may not be easy to make it work for both Apache and LiteSpeed at the same time by simply changing the rule order or removing some [L]s. You may try to make ruleset work for LiteSpeed, but it may break with Apache. In such cases, we would recommend leaving apache ruleset untouched enclosed with  ''<​IfModule !litespeed> ..</IfModule>''​ and create a new ruleset ​to fit with LSWS enclosed with ''<​IfModule litespeed> ..</IfModule>'' ​as a workable alternative.  ​Writing ​rewrite ​rules may require deep understanding how rewrite engine works and also pieces of arts about coding skillsEnd user's rule may be complicated,​ such as inheriting lengthy ruleset from a legacy Apache version or not following best practice or even involving some mistake/misunderstood, ​the final goal is to tweak existing ​rules set to work with LiteSpeed and Apache as quick as you can, instead of converting the ruleset ​to a piece of art.   +Let's say you have this ruleset: 
-  + 
-For example:+  RewriteEngine On 
 +  RewriteBase / 
 +  RewriteRule ^es/(.*)$ /$1 [L] 
 +  RewriteRule ^reservar\.html(.*)$ reservar.php ​[L] 
 +   
 +Under Apache
 +  - A visitor hits the URL ''<​nowiki>http://​example.com/​es/​reservar.html</nowiki>'' ​
 +  - The web server recognizes the pattern ''​^es/​(.*)$''​ from the first rewrite rule and the request is internally redirected ​to ''/​reservar.html''​. 
 +  - ''/​reservar.html''​ also matches the rule ''​^reservar\.html(.*)$''​ and is redirected to ''​reservar.php''​. 
 +  - The contents of this latest file (''​reservar.php''​) is sent to the visitor. 
 + 
 +Under LiteSpeed Web Server: 
 +  - A visitor hits the URL ''<​nowiki>http://​example.com/​es/​reservar.html</nowiki>''​ . 
 +  - The web server recognizes the pattern ''​^es/​(.*)$''​ from the first rewrite ​rule and the request is internally redirected to ''/​reservar.html''. 
 +  - Processing stops at the first [L] (''​RewriteRule ^es/(.*)$ /$1 [L]''​) and never reaches ​the forth line at all. 
 + 
 +If you want LSWS to process these rules with the same results ​as Apache, ​you can update them to use only one [L]: 
 + 
 +  RewriteEngine On 
 +  RewriteBase / 
 +  RewriteRule ^es/(.*)$ /es/$1 [NC] 
 +  RewriteRule ^/​es/​reservar.html(.*)$ reservar.php [L] 
 + 
 +==== Example 3: Use "<​IfModule litespeed>​ ... </​IfModule>"​ for Different Rules====  
 +Change this:
  
   RewriteCond %{REQUEST_URI} !\..*   RewriteCond %{REQUEST_URI} !\..*
Line 54: Line 94:
   RewriteRule (.*) http://​example.com/​$1 [R=301,L]   RewriteRule (.*) http://​example.com/​$1 [R=301,L]
  
-You can change it to:+To this:
  
   RewriteCond %{REQUEST_URI} !\..*   RewriteCond %{REQUEST_URI} !\..*
Line 98: Line 138:
   RewriteRule presenter/​(.*) presenter_$1 [L]   RewriteRule presenter/​(.*) presenter_$1 [L]
   </​IfModule>​   </​IfModule>​
-   + 
-Another example is:+==== Example 4: Use "<​IfModule litespeed>​ ... </​IfModule>"​ for Different Blocks==== ​   
 +Change this:
   RewriteEngine On   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} -f   RewriteCond %{REQUEST_FILENAME} -f
Line 110: Line 151:
   RewriteRule ^(specials|about_us|links|order_complete|view_cart|checkout).html$ $1.page [L,​QSA]   RewriteRule ^(specials|about_us|links|order_complete|view_cart|checkout).html$ $1.page [L,​QSA]
   ​   ​
-You can make changes like the following:+To this:
   <​IfModule !litespeed>​   <​IfModule !litespeed>​
   RewriteEngine On   RewriteEngine On
  • Admin
  • Last modified: 2019/01/03 16:21
  • by Jackson Zhang