【问题标题】:PHP 301 redirect multiple URLsPHP 301 重定向多个 URL
【发布时间】:2014-10-26 19:55:56
【问题描述】:

我正在尝试使用 php 301 重定向而不是 htaccess 来移动多个动态 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 
header("HTTP/1.1 301 Moved Permanently"); 
header("Location: http://www.new-website/new-folder/ *insert end of URL from current address bar here* "); 
?>

【问题讨论】:

  • 使用header("Status: 301 Moved Permanently")
  • 您应该考虑使用 .htaccess 来执行此操作,因为您的选项可能会导致 HTTP 标头拆分。
  • 使用 .htaccess 或 vhost conf 文件来定义你的重写,因为它在服务器上会轻很多。 (虽然我猜你不了解 rewrite 模块,这就是为什么你打算在 php 中这样做?)

标签: php redirect http-status-code-301


【解决方案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"); 

header("Status: 301 Moved Permanently");

【讨论】:

    【解决方案2】:
    $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。

    【讨论】:

      【解决方案3】:

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

      让我们试试吧。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多