【发布时间】: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