【问题标题】:rewrite url with a #用 # 重写 url
【发布时间】:2012-03-07 21:17:17
【问题描述】:

你如何用# in 重写一个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/test 之类的URL,因此您将被定向到该页面,但不会将您带到诸如http://www.example.com/index#testhttp://www.example.com/index.php#test 之类的相关标签。

【问题讨论】:

    标签: .htaccess url mod-rewrite url-rewriting


    【解决方案1】:

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

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

    注意: #(以及它之后的任何内容)不是对服务器的实际 HTTP 请求的一部分。 所以你不能引用它或在你的.htaccess中获取哈希值。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-07
      • 2012-01-16
      • 2010-11-11
      • 2015-03-15
      相关资源
      最近更新 更多