簡體   English   中英

PHP 301重定向多個URL

[英]PHP 301 redirect multiple URLs

我正在嘗試使用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?= 任何內容

我想在當前網址的第二個/后面插入任何內容

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

您要在標頭函數中設置一個變量,然后嘗試在其中調用函數。 標頭(帶有位置)也應該只提供一個重定向的位置。

第二個標頭調用將替換第一個標頭,因此您只能使用其中一個:

$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");
$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來實現。

您嘗試使用php的最佳方法。 只需創建一個臨時變量。 使用windows.location.href編寫自己的JavaScript重定向。 將那些腳本分配給該臨時變量並回顯它。

讓我們嘗試將對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM