在PHP中重定向[.htaccess-like Redirection]


Redirect in PHP [.htaccess like Redirection]

我已经更改了我的博客url。(我知道。htaccess可以做到这一点(

RewriteEngine on
RewriteRule (.*) http://my-new-website.com/$1 [R=301]

但我现在不能使用.htaccess(个人原因…我现在只能使用PHP(

我想使用PHP代码来实现这一点。

我想重定向

http://my-old-website.com/v/test/new.html

http://my-new-website.com/v/test/new.html

我在网上搜索了很多,并在stackoverflow上寻找了更多类似的问题,但我没有发现任何像这个这样的问题

只需使用Location标头。

header('Location: http://my-new-website.com' . $_SERVER['REQUEST_URI']);
header('Content-Type: text/html');
die('I have moved to my-new-website.com'); // for ancient browsers

PHP有函数来设置自定义头。您所要做的就是将"刷新"设置为不同的页面。

header('Refresh:10;url=newwebsite.php' . $_SERVER['REQUEST_URI']);

页面在一定时间后重定向PHP

您可以通过设置Location标头来完成。以下将添加给定的$_GET参数,并将其附加到URL:

$newUrl = "http://my-new-website.com/";
$currentPage = $_SERVER['PHP_SELF'];
//building the querystrging (if some GET variables where set):
$queryString = "";
if(count($_GET)) > 0) {
    $queryString = "?" . http_build_query($_GET);
} 
//set the header to redirect:
header('Location:'.$newUrl.$currentPage.$queryString);

即:重定向http://example.com/v/file1.php?key1=val1&key2=val2http://my-new-website.com/v/file1.php?key1=val1&key2=val2