使用 PHP 进行 301 或 302 重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9363760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:39:37  来源:igfitidea点击:

301 or 302 Redirection With PHP

php

提问by toomanyairmiles

I'm considering using the following code during a website launch phase to show users a down for maintenancepage while showing me the rest of the site.

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

Is there a way to show the correct 302 re-direction status to search engines or should I look for another .htaccessbased approach?

有没有办法向搜索引擎显示正确的 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');
};

回答by dynamic

For a 302 Found, i.e. a temporary redirect do:

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

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

If you need a permanent redirect, aka: 301 Moved Permanently, do:

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

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

For more info check the PHP manual for the header function Doc. Also, don't forget to call exit;when using header('Location: ');

有关更多信息,请查看头函数Doc的 PHP 手册。另外,exit;使用时不要忘记调用header('Location: ');

But, considering you are doing a temporary maintenance (you don't want that search engines index your page) it's advised to return a 503 Service Unavailablewith a custom message (i.e. you don't need any redirect):

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

<?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>

回答by Vitamin

The following code will issue a 301 redirect.

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

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

回答by binar

I don't think it really matters how you do it, from PHP or htaccess. Both will accomplish the same thing.

我认为从 PHP 或 htaccess 中如何做到这一点并不重要。两者都会完成同样的事情。

The one thing I want to point out is whether you want the search engines begin to index your site in this "maintenance" phase or not. If not, you could use the status code 503("temporarily down"). Here's a htaccess example:

我想指出的一件事是您是否希望搜索引擎在这个“维护”阶段开始索引您的网站。如果没有,您可以使用状态代码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]

In PHP:

在 PHP 中:

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

With the current PHP redirect code you're using, the redirect is a 302(default).

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

回答by Nanne

Did you check what header you're getting? Because you should be getting a 302with above.

你有没有检查你得到什么标题?因为你应该得到一个302与上面。

From the manual: http://php.net/manual/en/function.header.php

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

The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

第二个特殊情况是“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;
?>

回答by Rob Pridham

From the PHP documentation:

从 PHP文档

The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

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

so you are already doing the right thing.

所以你已经在做正确的事情了。

回答by Rob Pridham

save this file in the same directory as .htaccess

将此文件保存在与 .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