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

Space in value part of RewriteMap

· 52CE 我爱测

This question is related to Apache RewriteMap with URLs containing space doesn't work , but in this case the issue is a space in the value part of the map, not the key part. Say I have this entry: /a-normal-url/ /a-less-normal ur…

问题原文

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

This question is related to Apache RewriteMap with URLs containing space doesn't work, but in this case the issue is a space in the value part of the map, not the key part. Say I have this entry:

/a-normal-url/    /a-less-normal url/

As a RewriteMap's txt files are space separated, this won't work as-is. I've thus tried to escape the space:

/a-normal-url/    /a-less-normal%20url/

Unfortunately mod_rewrite will helpfully escape the redirect target, so this will lead to the request being redirected to /a-less-norma%2520url/, which also isn't what I want.

Just in case, I've also used a backslash to escape the space (/a-less-normal\ url/), but unsurprisingly this also didn't help (it will redirect to (/a-less-normal%5c) in that case.

已采纳解答

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

I've actually realized what the answer was when writing the question (classic case of rubber duck debugging).

Just add the NE flag in the RewriteRule that actually uses the rewrite map's output, somewhere along these lines:

# Define the RewriteMap
RewriteMap redirect_map "txt:redirect_map.txt"

# Look up the requested URI in the redirect map. If the result is not empty...
RewriteCond ${redirect_map:%{REQUEST_URI}} ^(.+)$

# ...then use the mapped value for the redirect, without percent-encoding it
RewriteRule . %1  [R=301,L,NE]

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

资料来源与适用范围

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

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

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