大网数据广告活动
服务器运维问答

Apache mod_rewrite RewriteRule loops despite L flag

· 52CE 我爱测

Apache/2.4.54. I am trying to achieve that similar URLs (e.g. "/anystuff.htm") are externally redirected to "/something" which internally is "something.html". But the following rules cause a loop instead of stopping, i.e. "someth…

问题原文

原帖链接 · handle · CC BY-SA 4.0

Apache/2.4.54.
I am trying to achieve that similar URLs (e.g. "/anystuff.htm") are externally redirected to "/something" which internally is "something.html". But the following rules cause a loop instead of stopping, i.e. "something" is matched

RewriteEngine On
RewriteBase /
RewriteRule ^something$ something.html [L]
RewriteRule ^(some|thing|any|stuff).*/?$ /something [L, R=301,NC]

But it seems the "L" directive does not have an effect, because the redirect to "/something" is matched again by the second rule, causing the loop.

Testing with https://htaccess.madewithlove.com/ suggests it should work as expected. I don't think I can enable logging :-(

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-11-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。