简体   繁体   English

.htaccess重定向锚页面/页面#anchor到url

[英].htaccess redirect anchor page/page#anchor to url

How do I redirect page/page#anchor to http://www.example.com/page/page . 如何将page/page#anchor重定向到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 彼得

That can't be done on server side since URI component after # is handled by client side only. 这不能在服务器端完成,因为#之后的URI组件仅由客户端处理。

You can alternatively use this Javascript code to do the same: 您也可以使用此Javascript代码执行相同的操作:

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

I believe the data after # is considered a fragment. 我相信#之后的数据被认为是片段。 Fragment data is not sent to the server. 片段数据不会发送到服务器。 Therefore you cannot capture it using modrewrite. 因此,您无法使用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. NE标志可以让您捕获^/page/page并重定向到http://www.example.com/page/page#anchor但它不会相反。

A simple JavaScript replace will replace the links and allow the users to click on them. 简单的JavaScript替换将替换链接并允许用户单击它们。 But that won't help you with SEO if that is your ultimate goal. 但如果这是你的最终目标,那对你的搜索引擎优化将无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM