用 # 重写 url

发布于 2025-01-06 09:19:50 字数 739 浏览 2 评论 0原文

如何用 # 重写 url?例如 http://www.example.com/index.php#test

在我当前的我有 .htaccess 文件:

RewriteEngine On    # Turn on the rewriting engine

RewriteRule  ^([^/.]+)/?$ $1.php [L] # rewrite without query string parameters
RewriteRule  ^([^/.]+)/([^/.]+)/?$ $1.php\#$2 [L] # one name string parameter

这将重写一个网址,例如 http://www.example.com/index/测试,以便您被定向到该页面,但不会带您到相关的标签如 http://www.example.com/index#testhttp://www.example.com/index.php#test 会。

How do you rewrite a url with a # in? e.g. http://www.example.com/index.php#test

In my current .htaccess file I have:

RewriteEngine On    # Turn on the rewriting engine

RewriteRule  ^([^/.]+)/?$ $1.php [L] # rewrite without query string parameters
RewriteRule  ^([^/.]+)/([^/.]+)/?$ $1.php\#$2 [L] # one name string parameter

This would rewrite a url such as http://www.example.com/index/test so you are directed to the page, but not take you to the relevant tag like http://www.example.com/index#test or http://www.example.com/index.php#test would.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生不灭 2025-01-13 09:19:50

我使用类似这样的方法将一个 url 重定向到另一个末尾带有哈希值的 URL:

RewriteRule ^([^/.]+)/([^/.]+)$ $1.php#$2 [R=301,L,NE]

注意: #(及其后面的任何内容)不是对服务器的实际 HTTP 请求的一部分。
因此您无法在 .htaccess 中引用它或获取哈希值。

哈希值永远不会发送到服务器,因此您无法将其与
mod_rewrite!

您必须使用例如 Javascript 来访问散列后的值!

I use something like this to redirect an url to another with a hash at the end:

RewriteRule ^([^/.]+)/([^/.]+)$ $1.php#$2 [R=301,L,NE]

Note: The # (and anything after it) is not part of the actual HTTP request to the server.
So you can't refer to it or get the hash value in you .htaccess.

The hash is never sent to the server, so you can't match it with
mod_rewrite!

You have to use e.g. Javascript to acces values after the hash!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文