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

Why is my Apache RewriteRule removing hex codes from the target url query string?

· 52CE 我爱测

I have an apache rewrite rule: RewriteRule ^/xy[/]{0,1}$ https://www.example.com/path/file.php?utm_source=Billboard&utm_medium=Print&utm_campaign=MyOrg%20Promo&utm_term=2020FA&utm_context=XY%2C%20border%20state%20abbreviation%20r…

问题原文

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

I have an apache rewrite rule:

RewriteRule ^/xy[/]{0,1}$ https://www.example.com/path/file.php?utm_source=Billboard&utm_medium=Print&utm_campaign=MyOrg%20Promo&utm_term=2020FA&utm_context=XY%2C%20border%20state%20abbreviation%20redirect [R]

But my target url is actually becoming:

https://www.example.com/path/file.php?utm_source=Billboard&utm_medium=Print&utm_campaign=MyOrg0Promo&utm_term=2020FA&utm_context=XYC0border0state0abbreviation0redirect

So, it is stripping out the %2 part of the hex codes in the query string of the target.

I have tried setting the PT, B, NE, QSD, & BNP flags. I'm on Apache 2.4.6(CentOS), and the BNP flag isn't supported there.

How can I get Apache to leave those codes alone?

已采纳解答

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

Because %2 is a backreference in mod_rewrite substitution strings, that would otherwise refer back to the 2nd captured subpattern in the last matched CondPattern. There is none in the example you posted, so this is replaced with an empty string - so it looks like it is simply "stripped".

For any sequence of %n (where n is 0-9), you need to backslash-escape the % to negate its special meaning. eg. \%20

Or, use + instead of %20 to escape literal spaces in the query string.

You will need the NE flag on the RewriteRule to prevent the query string being doubly escaped. (And you should probably be using the L flag as well.)

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

资料来源与适用范围

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

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

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