301重定向旧Ajax永久链接

时间:2016-02-15 14:09:56

标签: apache .htaccess redirect

我的客户旧网站正在运行Wix

Wix创建了这样的AJAX固定链接: old-domain.com /#!about / c20r9 而不是: old-domain.com/about

我通过new-domain.com为他们开发了一个新的WordPress网站。 old-domain.com已添加到new-domain.com作为域别名。

我想将旧网页网址从old-domain.com重定向到new-domain.com上的特定网页。例如: old-domain.com /#!about / c20r9 应重定向到: new-domain.com/about

我了解301重定向规则不起作用,因为服务器无法识别哈希网址。

如何将old-domain.com的旧网址重定向到new-domain.com?

1 个答案:

答案 0 :(得分:1)

正如我的评论中所提到的,JavaScript是一种可行的方式,因为片段(#...)永远不会发送到服务器。

在您的具体情况下,您可以使用以下JS代码相应地重定向您的页面。

自动模式:如果页面名称未更改,请使用:

// Get the current hash
var currentHash = window.location.hash;

// If there is one, and it is in fact a hashbang, redirect it to the new URI.
// This extracts the 'about' from '#!about/c20r9', for example, and assigns the
// location (/about) once extracted.
if (currentHash.indexOf('#!') == 0){
    window.location.assign(currentHash.replace(/^#!([^\/]+)\/.*/, '$1'));
}

手动模式:如果页面名称不同(将Wix与迁移的网站进行比较时),请使用此选项。此方法使用对象映射重定向,扫描对象,并在找到匹配项时重定向。

// Get the current hash
var currentHash = window.location.hash;

// If there is one, and it is in fact a hashbang, redirect it to the new URI,
// based on the array set out.
if (currentHash.indexOf('#!') == 0) {

    // Get the old page name from the hash
    var oldPageHash = currentHash.replace(/^#!([^\/]+)\/.*/, '$1');

    // Define the redirects (old and new counterparts)
    var redirects = {
        'foo-page': 'new-foo-page',
        'bar-page': 'new-bar-page',
    }

    // Loop through the redirects and set the location if there is a match
    for (var oldPage in redirects) {
        if (redirects.hasOwnProperty(oldPage)) {
            if (oldPage == oldPageHash) {
                window.location.assign(redirects[oldPage]);
            }
        }
    }

}