Php 301使用if/else语句重定向

Php 301使用if/else语句重定向,php,redirect,http-status-code-301,if-statement,Php,Redirect,Http Status Code 301,If Statement,我想根据if-else语句进行PHP301重定向。 我有2个类别,并希望在类别2中的网页重定向到另一个网址 是否可以将类别2的所有页面从domain1.com重定向到domain2.com 还有::domain1.com/pagetitle.html到domain2.com/pagetitle.html 我有以下几点,但似乎不起作用。in_类别可以,但重定向仅是基本url domain2.com,而不是domain2.com/pagetitle.html: if (in_category('2'

我想根据if-else语句进行PHP301重定向。 我有2个类别,并希望在类别2中的网页重定向到另一个网址

是否可以将类别2的所有页面从domain1.com重定向到domain2.com

还有::domain1.com/pagetitle.html到domain2.com/pagetitle.html

我有以下几点,但似乎不起作用。in_类别可以,但重定向仅是基本url domain2.com,而不是domain2.com/pagetitle.html:

if (in_category('2') ){
Header( "HTTP/1.1 301 Moved Permanently" ); 
header('Location: http://www.markett.nl' . $url_suffix . '/' . $url, true, 301); 
} else { }
--------------编辑--------------------

这仍然让我发疯

我有以下几点,但是firefox说通过一些cookie加载时出现问题

这是正确的标记吗

<?php $category = get_the_category();
$url = $_SERVER['REQUEST_URI'];

// Redirect to correct site on basis of section id
if(in_category('2') ){ 
header('Location: http://www.domain2.com/' . $url, true, 301); 
exit; } 
?>


我也被重定向了。

查看$\u服务器变量,找出您需要哪一个:

为了让事情变得更简单:

var_dump($_SERVER);
die();
在代码之前添加这个(一次),以了解变量由什么组成。我没有给出完整的答案,因为了解这些变量也很好。

问题 是否可以将类别2的所有页面从domain1.com重定向到domain2.com

可能的解决方案 在
header()之后调用
exit
您需要在发出标题后退出,以便立即发送

将状态代码传递到
header()
也可以将HTTP状态代码作为第三个参数传递,而不是对
header()进行两次调用。

强制HTTP响应代码为指定的值。请注意 参数仅在字符串不为空时有效

代码 因此,代码如下所示:

if(in_category('2') ){
    header('Location: http://www.domain2.nl' . $url_suffix . '/' . $url, true, 301);
    exit;
}

什么是
$url\u后缀
,什么是
$url
?另请参见。
标题('Location:…')
自动设置HTTP 301 btw。