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

How to simulate Apache [END] flag on a redirect?

· 52CE 我爱测

For business-specific reasons I created the following RewriteRule for Apache 2.2.22 (mod_rewrite): RewriteRule /site/(\d+)/([^/]+)\.html /site/$2/$1 [R=301,L] Which if given a URL like: http://www.example.com/site/0999/document.h…

问题原文

原帖链接 · Javier Méndez · CC BY-SA 4.0

For business-specific reasons I created the following RewriteRule for Apache 2.2.22 (mod_rewrite):

RewriteRule /site/(\d+)/([^/]+)\.html /site/$2/$1 [R=301,L]

Which if given a URL like:

http://www.example.com/site/0999/document.html

Is translated to:

http://www.example.com/site/document/0999.html

That's the expected scenario. However, there are documents which name are only numbers. So consider the following case:

http://www.example.com/site/0055/0666.html

Gets translated to:

http://www.example.com/site/0666/0055.html

Which also matches my RewriteRule pattern, so I end up with "The web page resulted in too many redirects" errors from browsers.

I have researched for a long time, and haven't found "good" solutions. Things I tried:

  1. Use the [END] flag. Unfortunately is not available on my Apache version nor it works with redirects.

  2. Use %{ENV:REDIRECT_STATUS} on a RewriteCond clause to end the rewrite process (L). For some reason %{ENV:REDIRECT_STATUS} is empty all the times I tried.

  3. Add a response header with the Header clause if my rule matches and then check for that header (see: here for details). Seems that a) REDIRECT_addHeader is empty b) headers are can't be set on the 301 response explicitly.

There is another alternative. I could set a query parameter to the redirect URL which indicates it comes from a redirect, but I don't like that solution as it seems too hacky.

Is there a way to do exactly what the [END] flag does but in older Apache versions? Such as mine 2.2.22.

已采纳解答

原帖链接 · Krist van Besien · CC BY-SA 3.0

Add a query parameter to the redirect is the best option. It is done quite often for all kinds of reasons.

For example make the rule like this:

RewriteCond %{QUERY_STRING}  !redirected
RewriteRule /site/(\d+)/([^/]+)\.html /site/$2/$1?redirected [R=301,L]

What now happens is that the redirects are to URLs with "?redirected" added to them. And so the rule isn't applied the second time.

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

资料来源与适用范围

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

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

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