我有一个页面(,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”(重定向)标志将URL更改为example.com/index.php#anchor,,但我希望URL为example.com/anchor.。
如果我去掉example.com锚标志并在浏览器中输入example.com/anchor,那么浏览器就会转到索引页,并保持干净的网址(/),但不会滚动到锚。
所以..。有没有办法在.htaccess中实现这一点,或者用JavaScript实现这一点有没有好的办法?
发布于 2014-11-08 00:39:52
使用URL重写不可能做到这一点。因为当#
是URL的一部分时,浏览器会向下滚动...而不是在服务器端。
发布于 2018-10-19 09:52:34
你不能用.htaccess
做到这一点。但您可以使用以下JavaScript代码来完成此操作:
window.location = String.prototype.replace(window.location, /index\.php#(.*)/, function(match, g1){return g1;});
https://stackoverflow.com/questions/26811952
复制