'hash' url rewrite in .htaccess

前端 未结 2 765
鱼传尺愫
鱼传尺愫 2020-11-27 19:20

I want to rewrite http://example.com/articles.html#first-article with http://example.com/articles/first-article

Is it possible to rewrite?<

相关标签:
2条回答
  • 2020-11-27 19:59

    The # can be added in the substitution URL with the NE flag. Check:

    • This Apache link that describes specifically how to redirect anchors.

    • This W3C Protocols link and this one.

    • This answer in SO is pretty complete.

    • Another answer in SO.

    • Another answer in SO (mine).

    So, you may try this:

    Options +FollowSymlinks -MultiViews
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI}  !articles\.html      [NC]
    RewriteCond %{REQUEST_URI}  ^/articles/([^/]+)/? [NC]
    RewriteRule .*       /articles.html#%1           [R,NE,L]
    

    Redirects

    http://example.com/articles/parameter

    To

    http://example.com/articles.html#parameter

    String articles is assumed to be fixed while parameter is assumed to be variable.

    0 讨论(0)
  • 2020-11-27 20:12

    No, it's not possible. The URL fragment (everything from # on) not even gets sent to the server.

    Maybe you want to consider a JavaScript based solution, where the actual content will be loaded via AJAX dependent on the fragment. You will find some useful information about this method here: What's the shebang/hashbang (#!) in Facebook and new Twitter URLs for?

    0 讨论(0)
提交回复
热议问题