Home  >  Article  >  Backend Development  >  How to implement 301 in php

How to implement 301 in php

藏色散人
藏色散人Original
2021-05-19 10:12:393629browse

php method to implement 301 jump: first open the corresponding PHP code file; then obtain the current domain name and determine whether there are parameters behind the address; finally pass "header('HTTP/1.1 301 Moved Permanently'); "Set the status code and implement 301 jump.

How to implement 301 in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

php implements 301 jump and 301 redirect Methods.

Normal temporary jumps in php are usually used:

header("Location:your_dest_url");

The returned status code is 302

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");

Attachment: 30*The difference between 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,

This What are the benefits or problems when using the 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 permanent redirection of the 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);//跳转到目标

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement 301 in php. 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