.htaccess - 用 # 重写 url

标签 .htaccess url mod-rewrite url-rewriting

如何用 # 重写 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

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

最佳答案

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

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

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

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

你必须使用例如Javascript 访问哈希后的值!

关于.htaccess - 用 # 重写 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9311888/

相关文章:

php - 使用 Mod Rewrite 更改 URL 结构

php - Lumen Routes 显示 404,root route 除外

c# - HttpUtility.UrlEncode - 减号而不是加号?

regex - 使用另一个 get params 重写查询字符串

apache - htaccess错误文档未呈现

.htaccess - htaccess 从旧论坛网址到新论坛网址

django - 如何使用自定义 django 404 错误 View 进行重定向?

java - 未知主机异常

regex - .htaccess - 强制 url 从 'dir/page.html' → 'dir/page'