【问题标题】:If Array/Table contains xyz, than redirect to its value (PHP? JS? htaccess?)如果 Array/Table 包含 xyz,则重定向到它的值(PHP?JS?htaccess?)
【发布时间】:2021-12-31 14:16:27
【问题描述】:

我将大约 100 篇文章从旧网站移至新网站。我想在旧站点的标头中创建重定向,因此如果要访问文章的旧 URL,则应将访问者重定向到该文章的新 URL。

但是,仅仅替换域并不能解决问题,因为我更改了文章的固定链接。所以我需要一些“数据库”(带有数组?)来决定实际 URL 是否在数据库中有重定向,例如:

// "the old permalink" = "the new permalink"
$urlpermalink["article-cars"] = "http://NewWebsite.com/new-cars-article";
$urlpermalink["an-article-dogs"] = "http://NewWebsite.com/new-dogs-text";
$urlpermalink["old-text-trees"] = "http://NewWebsite.com/new-blogcontent-about-trees";

例如,如果访问者访问“http://OldWebsite.com/article-cars”,他应该被重定向到“http://NewWebsite.com/new-cars-article”,因为那是数据库/数组的说法。

所以我可以这样:

$visitingurl = $_SERVER[REQUEST_URI]; // Getting the URL the visitor is on now

foreach( $urlpermalink as $value ) { // For every entry in the database/array...
    if (strpos($visitingurl, $urlpermalink) !== false) { // check if the visitingurl contains that (like "article-cars")
        // The visitor is indeed on an old URL which is in the database/array, so let's redirect him to the new URL
        header("HTTP/1.1 301 Moved Permanently");
        header(url . $_SERVER['QUERY_STRING']);
        exit();
    }
}

当然这段代码是完全错误的,但是我对PHP几乎一无所知,所以你能帮我解决这个问题吗? (JavaScript 解决方案也可以,或 htaccess 或其他任何 :))

非常感谢!

【问题讨论】:

    标签: php arrays .htaccess multidimensional-array


    【解决方案1】:

    我想我现在用 htaccess 解决了这个问题(我也不知道 htaccess,只是用谷歌搜索了更多)。

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RedirectMatch 301 ^/article-cars/ http://NewWebsite.com/new-cars-article
    RedirectMatch 301 ^/an-article-dogs/ http://NewWebsite.com/new-dogs-text
    RedirectMatch 301 ^/old-text-trees/ http://NewWebsite.com/new-blogcontent-about-trees
    </IfModule>
    

    这样可以吗?

    【讨论】:

    • 嗯,它有效吗?那就“好”了吧?
    • @arkascha 一个可行的解决方案仍然可能是一个糟糕的解决方案 :) 它可能会在以后引起问题,或者是关于安全等方面的问题。
    • 当然,没有任何陈述是绝对真实和众所周知的。但是您希望其他人如何回答这个问题?我们对您的具体情况或未来几乎一无所知。所以我们只能笼统地回答。重定向请求是可能的,这是一件正常的事情,它本身不会突然引发明显的安全问题。
    猜你喜欢
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    相关资源
    最近更新 更多