htaccess重定向#!在网址中

时间:2010-10-19 17:12:22

标签: ajax .htaccess url-rewriting

我正在使用ajax在我的网站上加载页面。 每次加载页面时,我都会将浏览器中的URL更改为

http//www.example.com/old/page/#!/new/page/

通过使用javascript设置window.loaction

现在我想要做的是当有人通过输入网址进入我的网站

http//www.example.com/old/page/#!/new/page/

他应该自动被重定向到

http//www.example.com/new/page/

这也有点发生在Facebook上。

有人可以帮助我使用所需的.htaccess代码来实现相同的目标。

提前致谢

1 个答案:

答案 0 :(得分:2)

我认为您网址中#符号后面的任何内容都不会在服务器端显示。所以htaccess,php等甚至都不知道哈希是否有开头。我认为为了解决这个问题,你将不得不使用客户端重定向。

window.onload = function(){
  // First we use a regex to check for the #! pattern in the hash     
  if(window.location.hash.match(/\#\!/i)){
    // If we found a match, use substring to remove the #! and do a redirect
    window.location = window.location.hash.substring(2);
  }
};

此示例将在页面加载时立即重定向用户。不幸的是,以这种方式进行重定向无助于搜索引擎重新索引您的网站,但这只是使用花哨的javascript或基于散列的URL的陷阱之一。