5

我见过许多相反的例子,但我希望从锚/哈希 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) 都没有看到它。

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

4

4 回答 4

13

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

于 2010-08-04T18:55:13.053 回答
2

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

var redirectFragment = window.location.hash.replace(/^#/,'');
if ( '' !== redirectFragment ) {
    window.location = 'http://new.html-site.com' + redirectFragment;
}
于 2012-05-17T16:37:52.617 回答
1

我是您链接到的帖子的作者。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];
}
于 2010-08-04T21:05:45.380 回答
0

我使用了@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-08-10T21:10:33.617 回答