AEM Dispatcher重定向:Apache Rewrite编码URL规则失效求助
Apache RewriteRule 重定向URL编码异常问题
我在AEM Dispatcher的重写配置文件中添加了多条规则,其中一条规则如下:
RewriteRule ^/myvanity$ https://www.somedomain.com/path/my-page.html?nextUrl=https%3A%2F%2Fwww.urlencoder.org%2F [NC,NE,R=301]
访问 vanity URL www.mydomain.com/myvanity 时,跳转后的地址出现编码错误:
https://www.somedomain.com/path/my-page.html?nextUrl=httpsAFFwww.urlencoder.orgF
这里的URL编码部分完全错误,重定向既没有保留原编码,解码后的结果也不符合预期
预期的正确跳转地址应该是:
https://www.somedomain.com/path/my-page.html?nextUrl=https://www.urlencoder.org/
其他格式的重写规则都能正常工作,比如:
RewriteRule ^/myvanity-2$ https://www.otherdomain.com/other-path/other-page.html [NC,NE,R=301] RewriteRule ^/myvanity-3$ /other-path-2/other-page.html [NC,NE,R=301] RewriteRule ^/myvanity-4$ https://www.otherdomain.com/other-path/other-page.html/#some-value [NC,NE,R=301]
问题原因与修复方案
原因分析
出现该问题是因为Apache RewriteRule在处理目标URL中的%时,会将%后的字符识别为内部变量(如%A、%F),而非普通的URL编码字符,即便添加NE标记也无法阻止这种内部解析逻辑。
修复方法
有两种可行的解决方式:
转义
%为%%
将目标URL中的每个%替换为%%,让Apache将其当作普通字符处理:RewriteRule ^/myvanity$ https://www.somedomain.com/path/my-page.html?nextUrl=https%%3A%%2F%%2Fwww.urlencoder.org%%2F [NC,NE,R=301]直接使用未编码URL配合
NE标记
既然预期结果是未编码的nextUrl=https://www.urlencoder.org/,可直接写入未编码地址,依靠NE标记避免Apache自动编码:RewriteRule ^/myvanity$ https://www.somedomain.com/path/my-page.html?nextUrl=https://www.urlencoder.org/ [NC,NE,R=301]
第二种方法更简洁直观,推荐优先使用。
内容的提问来源于stack exchange,提问作者Abhishek Sinha
相关产品推荐
相关产品推荐

