Home >Backend Development >PHP Problem >How does php return a 301 redirect?

How does php return a 301 redirect?

coldplay.xixi
coldplay.xixiOriginal
2020-07-01 11:24:003552browse

php returns a 301 redirect method: 1. Normal temporary jump method in php, the code is [header("Location:your_dest_url")]; 2. Set the status code before to implement php 301 jump, code is [HTTP/1.1 301 Moved].

How does php return a 301 redirect?

php method to return a 301 redirect:

1. Normal temporary jump in php Use:

header("Location:your_dest_url");

The returned status code is 302

2. If you want to implement php 301 jump, you need to set the status code before:

header( "HTTP/1.1 301 Moved Permanently" ) ; 
header("Location:your_dest_url");

Note: The difference between 30* return status codes

301, 302 are both HTTP status codes, and both represent that a certain URL has been transferred. The difference is :

  • 301 redirect: 301 represents Permanently Moved,

  • ##302 redirect: 302 represents Temporarily Moved ,

What are the benefits or problems when using these two transfers?

  • 301 redirection is the best way to be friendly to search engines after the webpage changes its address. As long as it is not a temporary move, it is recommended to use 301 for redirection.

  • 302 Redirect is a temporary transfer.

Use the above method to achieve

php 301 redirection and make a permanent redirection of url.

For example:

<?php
$the_host = $_SERVER[&#39;HTTP_HOST&#39;];//取得当前域名
$request_uri = isset($_SERVER[&#39;REQUEST_URI&#39;]) ? $_SERVER[&#39;REQUEST_URI&#39;] : &#39;&#39;;//判断地址后面是否有参数 
 
header(&#39;HTTP/1.1 301 Moved Permanently&#39;);//发出301头部
header(&#39;Location: http://www.jbxue.com&#39;.$request_uri);//跳转到目标

Related learning recommendations:

PHP programming from entry to proficiency

The above is the detailed content of How does php return a 301 redirect?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:What is php 503 error?Next article:What is php 503 error?