使用PHP进行301或302重定向 [英] 301 or 302 Redirection With PHP

查看:130
本文介绍了使用PHP进行301或302重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

$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');
};

推荐答案

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

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

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,请执行以下操作:

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

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

有关更多信息,请查阅PHP手册中的标题函数 Doc .另外,使用header('Location: ');

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

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

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 Unavailable with a custom message (i.e. you don't need any redirect):

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

这篇关于使用PHP进行301或302重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆