2011-04-09 93 views
0

我如何可以重定向如何在PHP中使用301標頭重定向頁面?

example.com/script.php?id=567 

用頭301重定向

example.com/script2.php?id=5677&something=anotherthing&something2=anotherthing2 

在PHP?

+2

?選擇一個請 – 2011-04-09 13:15:22

+0

我猜他想要PHP,因爲它的標籤是這樣的。 – Khez 2011-04-09 13:18:53

+0

是的,這是我想要的......我的壞。 – m3tsys 2011-04-09 13:28:03

回答

1

如果您有權訪問文件script.php,那麼你可以在上面添加以下代碼:

<?php 
$id = $_GET['id']; 

//Get your extra params from the database if needed... 

header ('HTTP/1.1 301 Moved Permanently'); 
header ('Location: http://example.com/script2.php?id='.$id.'&something=anotherthing&something2=anotherthing2'); //Append params retrieved from database here. 
die(); 
?> 
在PHP或mod_rewrite的
+0

。謝謝。 – m3tsys 2011-04-09 13:46:43

3

代碼本身很簡單:

<?php 
if($_SERVER['REQUEST_URI']=='/script.php?id=567'){ 
    header ('HTTP/1.1 301 Moved Permanently'); 
    header ('Location: http://example.com/script2.php?id=5677&something=anotherthing&something2=anotherthing2'); 
    die(); 
} 
?> 

您還可以使用$_SERVER['HTTP_HOST']獲得主機名example.com。在致電header()之前,您還必須確保腳本沒有任何輸出。

+0

確定但567只是一個例子...這是一個變量...(我的壞,我沒有指定) – m3tsys 2011-04-09 13:25:55

+0

你想重定向,在哪裏,通過什麼匹配? @col。彈片可能是對的。如果你的情況不是PHP特定的,你應該考慮使用[mod_rewrite](http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html) – Khez 2011-04-09 13:28:26

+0

以及...例如576它可能是一個ID一個數據庫(我有更多)我需要使用PHP重定向它。 – m3tsys 2011-04-09 13:32:44