Home > Backend Development > PHP Tutorial > PHP uses header+Location to implement website 301 redirection

PHP uses header+Location to implement website 301 redirection

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-28 08:28:29
Original
903 people have browsed it

The article is reproduced from PHP using header+Location to implement website 301 redirection | Lanzhou Raindance SEO | http://www.feiyuseo.com/seo-jishu/145.html

For our SEO staff, sometimes it is necessary Redirect the website. Generally speaking, the http return status codes for website redirection are 301 and 302. Let me introduce to you through examples how to use header+Location to implement website 301 redirection through PHP.

 PHP使用header+Location实现网站301重定向

Regarding the orientation problem of PHP websites, I searched on Baidu for a while and found that the most commonly used redirection method is the "header+Location" method. Below I give the most popular code:

<?php
header("Location: http://www.feiyuseo.com");
exit;
?>
Copy after login

But through my query of HTTP status code, I found that the return code after redirection through the above code is 302 status. Let’s first take a look at what is a 302 return code? The search engine returns a 302 error indicating that the requested resource has been temporarily transferred (Moved temporarily), and then a transferred URL will be given. When the browser handles the 302 error returned by the server, in principle, it will re-establish a TCP connection, and then Get the page of the redirected URL; but if the page exists in the cache, it will not be re-fetched. However, because black hat SEO abuses 302 redirects for cheating, 302 redirects have been regarded as spam and have been cracked down by major search engines. Therefore, the above code returning a 302 status is incomplete, and there is a risk of being blocked by search engines such as Baidu.

Please see the second method: return the 301 status code first and then redirect.

<?php
header(&#39;HTTP/1.1 301 Moved Permanently&#39;);
header(”Location: http://www.feiyuseo.com”);
exit;
?>
Copy after login

The above code can be said to solve the problem of using Location to generate 302 status, so it is a relatively complete and relatively safe website redirection method. Just when I was about to call it a day, I suddenly found another piece of jump code, which was also very good and more streamlined than this one. I will also attach it here and share it with you.

<?php
header("Location: http://www.feiyuseo.com",TRUE,301););
exit;
?>
Copy after login

Through the above line of code, the 301 redirection of the website is realized, and it runs efficiently, safely and reliably.

The above introduces how PHP uses header+Location to implement website 301 redirection, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template