繁体   English   中英

如何将example.com/index.php#anchor的URL重写为example.com/anchor?

[英]How do I rewrite the URL for example.com/index.php#anchor to example.com/anchor?

我有一个页面(example.com),在主要部分(example.com/index.php#anchor,example.com/index.php#about,example.com/index.php#contact等)锚以及作为子页面( example.com/subpage.php )。

这是我的.htaccess文件:

RewriteEngine On

DirectoryIndex index.php
ErrorDocument 404 http://example.com/my404page.php

RewriteRule ^anchor/?$ index.php#anchor [R,NE,L]
RewriteRule ^subpage/?$ subpage.php [NE,L]

子页面重写规则很有效。

对于锚链接,包括“ R ”(重定向)标志将URL更改为example.com/index.php#anchor但我希望URL为example.com/anchor

如果我删除了R标志并将example.com/anchor输入到我的浏览器中,那么它将转到索引页面并保留干净的URL(example.com/anchor), 但不会滚动到锚点

那么......有什么方法可以在.htaccess中实现这一点,还是有一种很好的方法来实现这一点?

使用URL重写不可能做到这一点。 因为当#是URL的一部分时浏览器向下滚动...不在服务器端。

你不能用.htaccess做到这一点。 但您可以使用以下JavaScript代码执行此操作:

window.location = String.prototype.replace(window.location, /index\.php#(.*)/, function(match, g1){return g1;});

暂无
暂无

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

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