htaccess 重定向 #!在网址中

发布于 2024-09-28 07:57:22 字数 432 浏览 0 评论 0原文

我正在使用 ajax 加载我网站上的页面。 每次加载页面时,我都会

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

使用 javascript 通过 window.loaction 设置

浏览器中的 url现在我想做的是,当有人通过输入 url 访问我的网站时,

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

他应该自动重定向到

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

这有点像Facebook 上也有这种情况。

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

提前致谢

I am using ajax to load pages on my website.
Each time a page is loaded I change the url in the browser to

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

by setting it through window.loaction using javascript

Now what I want to do is when someone comes to my website by entering the url

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

he should automatically get redirected to

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

This is somewhat that happens on facebook too.

Can someone help me out with the required .htaccess code to achieve the same.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花似海 2024-10-05 07:57:22

我认为 URL 中 # 符号之后的任何内容在服务器端都看不到。所以 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 的陷阱之一。

I don't think anything past the # symbol in your URL is even visible on the server side. So htaccess, php, etc won't even know the hash is there to begin with. I think in order to pull this off you're going to have to use a client side redirect.

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);
  }
};

This example will redirect the user immediately on page load. Unfortunately doing a redirect in this manner won't help the search engines to reindex your site, but thats just one of the pitfalls of using fancy javascript or hash based URL's.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文