In this article I will give you a quick overview of mod-rewrite L and END flags.
I will show you (with examples) how these two flags work and what’s the difference between L vs END .
What is L flag and how it actually works in htaccess
L is the most used flag in RewriteRule in htaccess. You can see it at the end of all RewriteRules in an htaccess file.
L means Last in RewriteRule. This flag indicates that the rule is last. If the rule has matched use it without looking in other RewriteRules.
L is the most commonly used flag in RewriteRule. But a few people know this that “L” actually has no effect in RewriteRule.
Your rule will work same even without it but using it at the end of every RewriteRule is a good practice as it makes your rule line look complete.
The official Apache mod-rewrite manual says that “Using L at the end of the rule tells server not to match other rules if the current one has matches the request successfully.
The following is an example according to the manual :
RewriteRule ^foo$ /demo.php [L] RewtiteRule (.*) /somefile.php [L]
The L has been used in the Rules above.
The first rule is for a particular URL path /foo and the second one is for all incoming paths.
Following the L flag explaination from manual you would think , the first rule above will match /foo and rewrite it to /demo.php . Done?
If you use the rules above in an htaccess file then your server will return an infinite loop error if you request /foo .
Since The second rule has a catch-all pattern, it matches the request from the first rule. L doesn’t work here as it’s just like a place holder.
Related post :
Mod-rewrite tutorial for beginners
Htaccess file has to load multiple times on server in order to process the request, so in the case above , if you type /foo the htaccess file loads and the first rule is triggered.
Then in order to process the rewrite destination file demo.php htaccess loads again. The second rule matches the demo.php and rewrites it to somefile.php . The internal Rewriting process doesn’t stop here.
To process the destination file somefile.php the htaccess loads again and the the pattern (.*) matches rewriting the somefile.php to itsef.
You can see the L the “last” flag no effect at all here. Its just serving its purpose as a placeholder here.
L flag stands for “Last” but it does not do its job in RewriteRule.
What is END flag used for in RewriteRule?
END is the most powerful flag in mod-rewrite.
END flag does what the L doesn’t
END flag ends all the internal rewriting process immideatly.
END flag available for use since Aoache 2.4 .You can’t use it on lower versions of Apache server . Using it on a lower version will result in a 500 server error.
In the following Rules we are using END instead of L .
RewriteRule ^foo$ /demo.php [END] RewtiteRule (.*) /somefile.php [END]
Now if your request /foo the rewriting will be processed smoothly without causing any error.
The END flag in the rule above terminates all the internal rewriting process as soon as the rule is matched and the destination file is served. This doesn’t tell the server to load htaccess again.
END flag has no effect with external redirection rules
Since END is made for internal URL Rewriting , You can use it with RewriteRules that rewrites the request internally.
Using is with a RewriteRule that use R flag or the full URL in destination has no effect at all. Because the flag END is for internal Rewriting.