What is L flag in htaccess rewrite rule
The [L] flag (or [last]) in Apache’s mod_rewrite tells the server to stop processing subsequent rewrite rules if the current rule matches. It acts as a “short-circuit,” preventing unnecessary processing of remaining rules in .htaccess or server configuration files. It is crucial for controlling redirection flow and preventing rule conflicts. Apache Software Foundation +2
Key Details About the [L] Flag:
- Stop Processing: If a
RewriteRulematches and[L]is applied, the engine immediately terminates the current round of rule processing. - Looping Behavior: While it stops the current pass,
mod_rewritemay still restart the entire rule-checking process (loop) if a URL was modified, allowing for multiple refinements. - Use Cases: Essential when you have finished redirecting a specific URL and do not want it to be altered by subsequent rules.
- Forced Application: The
[L]flag is automatically assumed if aRewriteRuleperforms an external redirect.Stack Overflow +2
Example:
apache
RewriteRule ^page1\.html$ /page2.html [L]
RewriteRule ^page2\.html$ /final-page.html [R=301]
If a user requests page1.html, the [L] flag ensures it is rewritten to page2.html and stops. Without [L], the second rule might turn page2.html into final-page.html immediately.
One Comment