.htaccess redirect anchor page/page#anchor to url

谁说胖子不能爱 提交于 2019-12-02 14:29:23

问题


How do I redirect page/page#anchor to http://www.example.com/page/page.

I tried the following code, but it's not working

RewriteRule ^page/page#anchor http://www.example.com/page/page [NE,L]

Could you please help me?

Peter


回答1:


That can't be done on server side since URI component after # is handled by client side only.

You can alternatively use this Javascript code to do the same:

<script>
if (location.href.indexOf("#") > -1) {
   location.assign(location.href.replace(/(page\/page)#anchor/i, "$1"));
}
</script>



回答2:


I believe the data after # is considered a fragment. Fragment data is not sent to the server. Therefore you cannot capture it using modrewrite.

the NE flag may let you capture ^/page/page and redirect to http://www.example.com/page/page#anchor but it won't work the other way around.

A simple JavaScript replace will replace the links and allow the users to click on them. But that won't help you with SEO if that is your ultimate goal.



来源:https://stackoverflow.com/questions/20503529/htaccess-redirect-anchor-page-pageanchor-to-url

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