2014-09-02 120 views
0

我想使用php 301重定向而不是htaccess移动多个动态URL。PHP 301重定向多个URL

我不能解决如何把它放在一起,虽然我有点出于我的深度,这是迄今为止。

<?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; "); 
?> 

旧网址:http://www.website.com/old-folder/news-article/full.php?=什么

新网址:http://www.new-website.com/new-folder/news-article/full.php?=什么

我想插入东西后当前URL的第二/即进入页眉位置

/old-folder /之后的任何内容每次都会有所不同。

简单来说,我试图做到这一点:

你尝试用PHP
<?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* "); 
?> 
+0

使用''标题(“状态:301永久移动”)'''。 – MrTux 2014-09-02 14:05:11

+0

你应该考虑使用.htaccess来做到这一点,因为你的选择可能倾向于HTTP头分割。 – MrTux 2014-09-02 14:06:29

+0

使用.htaccess或vhost conf文件来定义您的重写,因为它将在服务器上轻得多。 (虽然我猜你不明白重写模块,并且为什么你打算在php中做到这一点?) – user3791372 2014-09-02 14:11:39

回答

0

的最佳方式。只需创建一个临时变量。使用windows.location.href写你自己的JavaScript重定向。将这些脚本分配给该临时变量并进行回显。

让我们试试这对你有帮助。

1

您正在头文件函数中设置一个变量,然后尝试在那里调用函数。标题(包含位置)应该只被赋予一个位置来重定向。

第二集通话将替换第一个,所以你真的只能使用一个或另一个:

$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"); 
1
$url = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; 
$url = str_replace('http://www.website.com/old-folder/','',$url); 

希望你现在已经是这样的: news-article/full.php?=anything

您现在可以这样做:

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

但是,正如已经建议的那样,最好用Htaccess而不是php来解决这个问题。