【问题标题】:How to Detect and Redirect from URL with Anchor Using mod_rewrite/htaccess?如何使用 mod_rewrite/htaccess 从带有锚点的 URL 检测和重定向?
【发布时间】:2010-08-04 18:36:21
【问题描述】:

我见过许多相反的例子,但我希望从锚/哈希 URL 转到非锚 URL,如下所示:

From: http://old.swfaddress-site.com/#/page/name
To:   http://new.html-site.com/page/name

http://karoshiethos.com/2008/07/25/handling-urlencoded-swfaddress-links-with-mod_rewrite/ 中的所有示例都不适用于我。听起来REQUEST_URI 里面有/#/stuff,但我和我的 Apache (2.0.54) 都没有看到它。

有什么想法、过去的经验或成功吗?

【问题讨论】:

  • 该页面正在讨论解决 Apple 产品中的错误,其中 # 被错误编码,因此意外传递到服务器。通常,正如 Wrikken 所说,这不会发生,因此您无法阅读片段。

标签: apache .htaccess mod-rewrite anchor swfaddress


【解决方案1】:

# 之后的任何内容都是一个片段,并且将不会发送到网络服务器。您无法在任何时候捕获它,您必须使用客户端方法来捕获它们。

【讨论】:

    【解决方案2】:

    @RobRuchte : 使用 window.location.hash 替换而不是正则表达式不是更好吗?

    var redirectFragment = window.location.hash.replace(/^#/,'');
    if ( '' !== redirectFragment ) {
        window.location = 'http://new.html-site.com' + redirectFragment;
    }
    

    【讨论】:

      【解决方案3】:

      我是您链接到的帖子的作者。 Wrikken 是正确的,命名锚点之后的内容不会发送到服务器,除非沿途有东西破坏了 URL。在客户端,您需要在登录页面中使用类似这样的 JavaScript 来将 swfaddress 链接重定向到另一个域上的相应 URL:

      var re = new RegExp('#(.*)');
      var redirectFragment = re.exec(document.location.toString());
      if (redirectFragment!=null)
      {
          document.location = 'http://new.html-site.com'+redirectFragment[1];
      }
      

      【讨论】:

        【解决方案4】:

        我使用了@m14t 答案的修改版本。这适用于看起来像 http://example.com/path/to/page#fragment --> http://example.com/path/to/page/fragment 的重定向>。请注意,我还为重定向连接了window.location.pathname,否则我将无法获得重定向的完整路径。如果新文件路径与旧文件路径完全不同,那么这将不起作用。

        var redirectFragment = window.location.hash.replace(/#/,'/');
        if ( '' !== redirectFragment ) {
            window.location = 'http://example.com' + window.location.pathname + redirectFragment;
        }
        

        就我而言,我需要将碎片链接构建到单个页面中,这是改善网站 SEO 的常用方法的一部分。

        【讨论】:

          猜你喜欢
          • 2017-01-26
          • 2012-05-13
          • 1970-01-01
          • 1970-01-01
          • 2018-06-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多