Home > Backend Development > PHP Tutorial > How to implement 301 permanent redirection in PHP_PHP tutorial

How to implement 301 permanent redirection in PHP_PHP tutorial

WBOY
Release: 2016-07-13 17:14:09
Original
1005 people have browsed it

This article will introduce how to implement 301 permanent redirection in PHP to point the domain name without www to the domain name with www.

When your site domain name changes or the web page address changes (the web page storage directory changes or the name of the web page file changes), how to ensure that the site’s traffic from search engines will not be lost during the transition period, and the search engine can be updated at the same time The web address in the results? 301 redirects are an ideal solution to this problem, as they direct users and search engines to the correct web page

The code is as follows
 代码如下 复制代码

$the_host = $_SERVER['HTTP_HOST'];//取得进入所输入的域名
$request_url = isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:'';//判断地址后面部分
if($the_host !== 'www.bKjia.c0m')//这是我要以前的域名地址
{ header('HTTP/1.1 301 Moved Permanently');//发出301头部
header('Location: http://www.bKjia.c0m'.$request_url);//跳转到我的新域名地址
}
?>

Copy code

$the_host = $_SERVER['HTTP_HOST'];//Get the entered domain name
$request_url = isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:'';//Judge the back part of the address
if($the_host !== 'www.bKjia.c0m')//This is the previous domain name address I want
{ header('HTTP/1.1 301 Moved Permanently');//Send 301 header
header('Location: http://www.bKjia.c0m'.$request_url);//Jump to my new domain name address
}
?>

 代码如下 复制代码

RewriteEngine on
RewriteRule ^(.*)$ http://www.bKjia.c0m/ [R=301,L]

1. Add 301 redirection directive to the .htaccess file

Using "mod_rewrite" technology, in the form:

The code is as follows Copy code
RewriteEngine on
RewriteRule ^(.*)$ http://www.bKjia.c0m/$1 [R=301,L] http://www.bkjia.com/PHPjc/629013.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629013.htmlTechArticleThis article will introduce PHP to implement 301 permanent redirection to point the domain name without www to the domain name with www. method is derived. When your site domain name changes or the web page address changes (web page...
source:php.cn
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