The L
flag only stops the current pass through the rewrite engine, it does not stop all processing. The directives that follow are not processed immediately, but (in a directory context) the rewriting process then starts over... During the "second pass" the first rule no longer matches (since the input is now something.html
) but the second rule does, so triggers the redirect.
(The rewrite engine effectively "loops" until the URL passes through unchanged. The directives are easier to understand when used in a server (non-directory) context, where the L
flag does effectively stop all processing, since there is only a single pass by the rewrite engine.)
However, on Apache 2.4 you can use the END
flag instead, to stop all processing by the rewrite engine (in a directory context). For example:
RewriteEngine On
RewriteRule ^something$ something.html [END]
RewriteRule ^(some|thing|any|stuff) /something [R=301,NC,L]
(Note that you had an erroneous space in the flags argument on the second rule. This would have resulted in a parse error, so I assume this was a typo in your question?)
The trailing regex on the second rule (ie. .*/?$
) is superfluous.
Using L
with a redirect (R
flag) is the same as END
. All processing stops.
The RewriteBase
directive is superfluous in your example.
NB: You should test first with a 302 (temporary) redirect and only change to a 301 (permanent) when you have confirmed that this works as intended. 301s are cached persistently by the browser so can make testing problematic. Consequently, you will need to clear the browser (and any intermediary) caches before testing.
Testing with https://htaccess.madewithlove.com/
suggests it should work as expected.
Unlike a real server, the MWL tester only (effectively) makes a "single pass" through the rules so cannot detect rewrite/redirect loops.
Aside: Ordinarily, you should arrange external redirect directives before internal rewrites. However, the second rule in your example would redirect /something
to itself, so your two directives are currently dependent on the order you have them in.