简体   繁体   English

PHP 301重定向多个URL

[英]PHP 301 redirect multiple URLs

I am trying to move multiple dynamic URLs using a php 301 redirect, not htaccess. 我正在尝试使用php 301重定向而不是htaccess移动多个动态URL。

I can't work out how to put it together though as I'm a little out of my depth, this is all have so far. 我想不出如何将其组合在一起,尽管由于我的深度不够,到目前为止一切都还不错。

<?php 
header("HTTP/1.1 301 Moved Permanently"); 
header("Location: http://www.new-website/new-folder/ $url="http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$url=str_replace('http://www.website.com/old-folder/','',$url);
echo $url; "); 
?>

Old URLs: http://www.website.com/old-folder/news-article/full.php?= anything 旧网址: http//www.website.com/old-folder/news-article/full.php?= 任何内容

New URLs: http://www.new-website.com/new-folder/news-article/full.php?= anything 新网址: http : //www.new-website.com/new-folder/news-article/full.php?= 任何内容

I want to insert anything that comes after the second / of the current URL into the Header Location 我想在当前网址的第二个/后面插入任何内容

Anything after /old-folder/ will be different each time. / old-folder /之后的任何内容每次都不同。

In simple terms I am trying to do this: 简单来说,我正在尝试这样做:

<?php 
header("HTTP/1.1 301 Moved Permanently"); 
header("Location: http://www.new-website/new-folder/ *insert end of URL from current address bar here* "); 
?>

You are setting a variable inside the header function, then trying to call functions inside there too. 您要在标头函数中设置一个变量,然后尝试在其中调用函数。 The header (with location) should just be given a location to redirect too. 标头(带有位置)也应该只提供一个重定向的位置。

The second header call will replace the first one, so you can only really use one or the other: 第二个标头调用将替换第一个标头,因此您只能使用其中一个:

$url = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$url = str_replace('http://www.website.com/old-folder/','',$url);

header("Location: http://www.new-website/new-folder/$url"); 

OR 要么

header("Status: 301 Moved Permanently");
$url = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$url = str_replace('http://www.website.com/old-folder/','',$url);

Hopefully you now have something like: news-article/full.php?=anything 希望您现在有类似的东西: news-article/full.php?=anything

You can now do: 您现在可以执行以下操作:

header("HTTP/1.1 301 Moved Permanently"); 
header("Location: http://www.new-website/new-folder/".$url); 

But, as suggested already, its best to approach this with Htaccess instead of php. 但是,正如已经建议的那样,最好使用Htaccess而不是php来实现。

Best way you try with php. 您尝试使用php的最佳方法。 Just create a one temp variable. 只需创建一个临时变量。 Write you own JavaScript redirection using windows.location.href. 使用windows.location.href编写自己的JavaScript重定向。 Assign those script to that temp variable and echo it. 将那些脚本分配给该临时变量并回显它。

Let's try that will help you. 让我们尝试将对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM