如何用 # 重写 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#test或http://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/