简体   繁体   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?

I have a page ( example.com ) with anchors at major sections ( example.com/index.php#anchor , example.com/index.php#about , example.com/index.php#contact , etc.) as well as subpages ( example.com/subpage.php ). 我有一个页面(example.com),在主要部分(example.com/index.php#anchor,example.com/index.php#about,example.com/index.php#contact等)锚以及作为子页面( example.com/subpage.php )。

Here's my .htaccess file: 这是我的.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]

The subpage rewrite rule works great. 子页面重写规则很有效。

For the anchor link, including the " R " (redirect) flag changes the URL to example.com/index.php#anchor , but I want the URL to be example.com/anchor . 对于锚链接,包括“ R ”(重定向)标志将URL更改为example.com/index.php#anchor但我希望URL为example.com/anchor

If I remove the R flag and enter example.com/anchor into my browser, than it goes to the index page and keeps the clean URL (example.com/anchor) but doesn't scroll to the anchor . 如果我删除了R标志并将example.com/anchor输入到我的浏览器中,那么它将转到索引页面并保留干净的URL(example.com/anchor), 但不会滚动到锚点

So... Is there any way to achieve this in .htaccess, or is there a good way to achieve this with JavaScript? 那么......有什么方法可以在.htaccess中实现这一点,还是有一种很好的方法来实现这一点?

It's not possible to do that with URL rewrite. 使用URL重写不可能做到这一点。 Because the browser scroll down when the # is part of the URL... not on the server side. 因为当#是URL的一部分时浏览器向下滚动...不在服务器端。

You can't do this with .htaccess . 你不能用.htaccess做到这一点。 But you can do it with the following JavaScript code: 但您可以使用以下JavaScript代码执行此操作:

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

暂无
暂无

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

相关问题 如何使用.htaccess将http://example.com/test.php设置为http://example.com/index.php?do=test? - How to make http://example.com/test.php to http://example.com/index.php?do=test using .htaccess? 如何将example.com/forum/index.php重定向到example.com/forum/ - How to redirect example.com/forum/index.php to example.com/forum/ 如何将example.com/index.php/lib更改为example.com/lib? - How to change example.com/index.php/lib to example.com/lib? 如何从这个example.com/index.php?lang=en更改我的多语言网址到这个example.com/en/? - How can I change my multilingual urls from this example.com/index.php?lang=en to this example.com/en/? 在LAMP服务器上,我希望将URL http://example.com/index.php重写为http://example.com。 我怎么做? - On a LAMP server, I want the URL http://example.com/index.php to be rewritten to http://example.com. How do I do that? Ubuntu-如何直接将example.com/opencart/index.php定向到http://example.com/和www.example.com? - Ubuntu - How direct example.com/opencart/index.php to http://example.com/ and www.example.com? htaccess将example.com/user.php?id=username重写为example.com/username - htaccess rewrite example.com/user.php?id=username to example.com/username 将example.com/dashboard.php更改为example.com/index.php?action=dashboard - Change example.com/dashboard.php to example.com/index.php?action=dashboard 是否可以将 example.com/index.php?page=abc 重定向到 example.com/abc? - Is it possible to redirect the example.com/index.php?page=abc to example.com/abc? 使用,htaccess将example.com?id=12重定向到example.com/index.php?id=12 - using ,htaccess redirect example.com?id=12 to example.com/index.php?id=12
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM