如何重定向到包含锚点的网址(#)?

时间:2009-08-18 00:15:47

标签: mod-rewrite encoding url-rewriting

我需要使用标准的传入URL并且重写将它们重定向到单个页面上的特定锚点(传入的html页面名称成为锚点名称)。例如,规则是这样的:

RewriteRule ^files/([a-zA-Z0-9\-_]+)\.html$ /test.html#$1 [R]

所以这个网址:

http://foo.com/files/bar.html

......将成为:

http://foo.com/test.html#bar

规则本身工作正常,但#字符在浏览器地址栏中编码为:

http://foo.com/test.html%23bar

当然不能正常作为锚点。是否有一种方法在.htaccess中强制它不对哈希进行编码?我也尝试像\#一样逃避它,但没有区别。

2 个答案:

答案 0 :(得分:9)

尝试规则中的noescape(NE)标志:

RewriteRule ^files/([a-zA-Z0-9\-_]+)\.html$ /test.html#$1 [NE, R]

答案 1 :(得分:5)

使用NE或No Escape标志:

RewriteRule ^files/([a-zA-Z0-9\-_]+)\.html$ /test.html#$1 [NE,R]