---
title: "请重视301、302跳转的PHP实现"
author: "muyu"
date: "2010-08-19T14:16:23Z"
tags: []
categories: ["编程语言"]
slug: "301-302-php"
draft: false
lastmod: "2011-09-04T17:40:25Z"
id: 741108
url: "https://muyu.iteye.com/blog/741108"
---

通常PHP的页面重定向代码：

```
header('Location: http://google.com/'); //不推荐！
```

 上述代码忽略了搜索引擎对于链接价值的转移，它会被当做“暂时地”重定向。

 

如果旧地址不存在了，要永久地更新到新地址，推荐的做法：

```
header('HTTP/1.0 301 Moved Permanently');
header('Location: http://google.com/');
```

还有一种比较好的方法，header已经考虑了：

```
header('Location: http://google.com/', true, 301);
```

 

作为类比，当服务器挂掉，以下代码较为友好地通知了搜索引擎“这只是一个暂时的技术问题”。

```
header('HTTP/1.0 500 Internal Server Error');
echo '站点维护中，我们将在2010-10-2日恢复!';
exit;
```

 

**参考**

\------

《搜索引擎优化高级编程（PHP版）》