Home >Backend Development >PHP Problem >What is the method to implement 301 jump in php

What is the method to implement 301 jump in php

青灯夜游
青灯夜游Original
2021-05-26 18:20:552449browse

How to implement 301 jump in php: First use the "header("HTTP/1.1 301 Moved Permanently");" statement to set the 301 status code; then use the "header("Location:your_dest_url");" statement Just jump.

What is the method to implement 301 jump in php

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

Normal temporary jump in php Transfer usually uses:

header("Location:your_dest_url");

The returned status code is 302

If you want to implement If php 301 jumps, 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 The encoding of HTTP status all represents 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.

Using the above method can be achieved PHP 301 redirect, permanent redirection of URL.

For example:

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

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the method to implement 301 jump 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