search
Homephp教程php手册PHP实现301重定向并通知搜索引擎的代码

PHP实现301重定向并通知搜索引擎的代码

Jun 13, 2016 am 11:37 AM
301phpcodeaccomplishsearch enginenowBaiduofnetnotifyRedirect

现在的搜索引擎非常挑剃特别是百度,如果你的网站或博客不是很出名,那么他在搜索你网页的时候就要对比看看有没有相同的内容,如果有他就不收录了。所以对于小网站或个人博客如果域名改变那么你就要通知搜索引擎,这样搜索引擎才能进行调整和更换。

以我的实际操作为例:原来某会员的文章都存放在http://bkjia.com/blog/member/这个目录中。这个member就是会员的名字,这样有一百个会员就有一百个这样的目录。现在二级域名已开通,站内的所有链接已经变成了新开通的二级域名 http://member.bkjia.com/这种形式。同样,member 也是不同会员的用户名。我需要做的时怎样通知搜索引擎,让搜索引擎中原来那样的链接改成开通二级域名后的链接,这样的好处就是搜索引擎不会因为你的新域名下的文章和老链接下文章有雷同,而放弃对你新域名下文章的收录。


//进行301重定向

$member="member";
$hywzdizhi="http://".$member."bkjia.com/";
$uri=$_SERVER[\'REQUEST_URI\'];
$amember="blog/$member/";
if (stristr($uri,"$amember")){
$pieces = explode("$amember", $uri);
$wangzhi = "$pieces[1]";
$tiaozhuan=$hywzdizhi.$wangzhi;

Header( "HTTP/1.1 301 Moved Permanently" ) ;
Header( "Location: $httptiaozhuan" );

}

?>

以上的命令就可以实现被访问地址为原来的http://bkjia.com/blog/member/ 以及这个链接目录中的所有网页时。

不但自动跳到最已启用的二级域名 http://member.bkjia.com/ 中,而且还通知了搜索引擎,把原来那个链接换成新的吧,原来的我不用了,这就是301重定向的规责。

简单说一下上面代码的原理。一、用$_SERVER[\'REQUEST_URI\']取到域名后的网址,如果网址中包含 blog/$member/ 则对网址进行拆分,取后面的链接放到准备好的 http://".$member."bkjia.com/ 这个二级域名下。二、发送301重定向命令,重定向的网址就是那个由二级域名开头组合后的网址。上面的命令很简单,根本没用正则,希望对你有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.