Try something like this untested example, using multiple rewrites where each rewrite excludes the conditions of the other:
example.com {
rewrite /l/ { # use basepath to avoid unnecessary regex checking
r ^/l/([a-zA-Z0-9/]+)$
to /l.php?i={1}
}
rewrite { # use not_starts_with to exclude other rewrite condition
if {uri} not_starts_with /l/
r ^/([a-zA-Z0-9-]+)$
to {uri}.php
}
}
Can I confirm, then that only one rewrite is ever executed? I must admit that I started out with the idea that multiple rewrites could be executed successively (perhaps requiring the use of {rewrite_path} to carry the current state from one to the next). This would certainly make some things easier, but I can see why the design might be otherwise.
It could be useful to mention this in the documentation of rewrite to avoid confusion for others.
That’s correct, @pwhodges. The rewrite middleware is called relatively early on in the chain and it picks the first valid (i.e. passes if and regex checks) rewrite rule with the longest base path to execute before passing on to the next middleware.
So strictly speaking, if {uri} not_starts_with /l/ is unnecessary in the above example, but included for readability; since only one rewrite would run, it will either be the first when its conditions are met, or the second otherwise.