How to Redirect to a url containing an anchor (#)?

会有一股神秘感。 提交于 2019-12-18 12:02:08

问题


I need to take standard incoming urls and rewrite redirect them to target specific anchors on a single page (incoming html page name becomes the anchor name). E.g., the rule would be like this:

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

So this url:

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

...would become:

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

The rule itself works fine, but the # character gets encoded in the browser address bar to:

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

which of course does not work correctly as an anchor. Is there a way in .htaccess to force it not to encode the hash? I also tried escaping it like \# but that makes no difference.


回答1:


Try the noescape (NE) flag in your rule:

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



回答2:


Use NE, or No Escape flag:

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



来源:https://stackoverflow.com/questions/1291158/how-to-redirect-to-a-url-containing-an-anchor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!