URL rewriting - removing hash

我的梦境 提交于 2019-11-27 07:04:19

问题


How can I remove the hash sign (#) from the page's URL ? I am using the SWFAddress plugin (jQuery) for deep linking purposes.

I need to replace this

localhost/site/#blog

by

localhost/site/blog

(Yes, #blog is just an anchor).

Somehow url rewriting in .htaccess doesn't work

RewriteRule /blog #blog [L]

Any suggestions ?


回答1:


The bit with the hash in the URL is not sent to the server when requesting a page, so you can't use redirect rules like that. It's client-side only.




回答2:


As the URL fragment is not transmitted to the server, you can only use a client side solution. Here’s one using JavaScript:

if (location.href.indexOf("#") > -1) {
    location.assign(location.href.replace(/\/?#/, "/"));
}

This simply checks if there is a # in the URL and replaces the first occurrence with /. So /site/#blog would get /site/blog.



来源:https://stackoverflow.com/questions/1991626/url-rewriting-removing-hash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!