我在要重定向的网站中有一个博客。
例如,此 URL https://example.com/blog/june-2021/name-of-blog-post应重定向到https://example2.com/blog/june-2021/name-of-blog-post
有数百篇博客文章,所以我无法使用数组来一一重定向。
我使用万神殿作为主机。我将脚本添加到 settings.php。
目前来自 example.com 的所有内容都转到 example2.com,因此无法正常工作。
这是我的脚本:
$url = $_SERVER['REQUEST_URI'];
$uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri_segments = explode('/', $uri_path);
$blog = $uri_segments[0];
$post_date = $uri_segments[1];
$post_name = $uri_segments[2];
if ((stripos($url, "/blog/") === 0) && (php_sapi_name() != "cli")) {
header('HTTP/1.0 301 Moved Permanently'); header("Location: https://example2.com".$blog.$post_date.$post_name);
if (extension_loaded('newrelic')) {
newrelic_name_transaction("redirect");
}
exit(); }
提前致谢!