使用PHP进行301或302重定向


73

我正在考虑在网站启动阶段使用以下代码向用户显示维护页面,同时向我显示该网站的其余部分。

有没有一种方法可以向搜索引擎显示正确的302重定向状态,还是应该寻找其他.htaccess基于方法的方法?

$visitor = $_SERVER['REMOTE_ADDR'];
if (preg_match("/192.168.0.1/",$visitor)) {
    header('Location: http://www.yoursite.com/thank-you.html');
} else {
    header('Location: http://www.yoursite.com/home-page.html');
};

14
记住exit这些标头之后的脚本
维生素

Answers:


137

对于302 Found,即临时重定向,请执行以下操作:

header('Location: http://www.yoursite.com/home-page.html');
// OR: header('Location: http://www.yoursite.com/home-page.html', true, 302);
exit;

如果您需要永久重定向,又名:301 Moved Permanently,请执行以下操作:

header('Location: http://www.yoursite.com/home-page.html', true, 301);
exit;

有关更多信息,请查看PHP手册中的文件Doc。另外,exit;使用时别忘了打电话header('Location: ');

但是,考虑到您正在进行临时维护(您不希望搜索引擎将您的页面编入索引),建议您返回503 Service Unavailable带有自定义消息的消息(即您不需要任何重定向):

<?php
header("HTTP/1.1 503 Service Unavailable");
header("Status: 503 Service Unavailable");
header("Retry-After: 3600");
?><!DOCTYPE html>
<html>
<head>
<title>Temporarily Unavailable</title>
<meta name="robots" content="none" />
</head>
<body>
   Your message here.
</body>
</html>

7
语法是void header ( string $string [, bool $replace = true [, int $http_response_code ]] )- php.net/manual/en/function.header.php
维生素

3
OP要求在维护期间进行重定向,在这种情况下,他必须使用302(而不是301)进行临时重定向。如果是301(也称为永久重定向),浏览器将永远不会尝试该页面,而是转到重定向的页面。
七点

1
有人会提到这个“ yoursite.com”只是使我的防病毒盾报告了几个“已检测到威胁”吗?为什么不在答案中使用example.com。帮帮我们,将您的帖子更新到example.com,因为yoursite.com是恶意软件。当我测试您的代码时,它会将我重定向到该恶意软件。请输入正确的答案,但是认真地解决此问题。
迈克尔d

3
@michaeld永远不要将占位符代码当作生产代码来进行测试。答案中使用的示例URL无关紧要-在运行代码之前,应始终替换自己的已知URL。
Janus Bahs Jacquet

关键是:我们应该使用302重定向客户端,然后发布503页,还是应该设置一个重写规则直接将503页发布到每个客户端的请求?
tonix

18

以下代码将发出301重定向。

header('Location: http://www.example.com/', true, 301);
exit;

7

我认为从PHP或htaccess的操作方式并不重要。两者将完成同一件事。

我想指出的一件事是,您是否希望搜索引擎在此“维护”阶段开始为您的网站建立索引。如果不是,则可以使用状态代码503(“暂时关闭”)。这是htaccess的示例:

RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !=503
RewriteCond %{REMOTE_HOST} ^192\.168\.0\.1
ErrorDocument 503 /redirect-folder/index.html
RewriteRule !^s/redirect-folder$ /redirect-folder [L,R=503]

在PHP中:

header('Location: http://www.yoursite.com/redirect-folder/index.html', true, 503);
exit;

使用当前使用的PHP重定向代码,重定向为302(默认)。


我不确定PHP(或底层服务器)是否允许将503状态代码与Location标头一起使用!实际上,我几乎可以肯定不会。(我还隐约记得在编写代理时我也犯了类似的错误。)您很可能应该使用header("HTTP/1.0 503 Service not available");@dynamic示例中所示的形式。
Sz。

3

您是否检查了获得的标题?因为您应该在302上面得到一个。

从手册中:http : //php.net/manual/zh/function.header.php

第二种特殊情况是“ Location:”标题。它将不仅将此标头发送回浏览器,而且还向浏览器返回REDIRECT(302)状态码,除非已经设置了201或3xx状态码。

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

3

从PHP文档

第二种特殊情况是“ Location:”标题。它将不仅将此标头发送回浏览器,而且还向浏览器返回REDIRECT(302)状态码,除非已经设置了201或3xx状态码。

所以您已经在做正确的事。


1

将此文件保存在与.htaccess相同的目录中

RewriteEngine on
RewriteBase / 

# To show 404 page 
ErrorDocument 404 /404.html

# Permanent redirect
Redirect 301 /util/old.html /util/new.php

# Temporary redirect
Redirect 302 /util/old.html /util/new.php
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.