search
Homephp教程php手册PHP教程:如何实现301重定向

PHP教程:如何实现301重定向

Jun 13, 2016 am 11:33 AM
301phpWhatoccurVarietyaddresshowaccomplishTutorialhouryeswebsiteRedirect

什么是301重定向?301重定向就是当你的网站地址发生变化时,例如更换域名,网站改版等等,你希望访客或者搜索引擎跳到新的地址上访问网站,这时就需要你做301重定向了。实现301重定向有很多方法,本文主要介绍PHP程序怎么实现301重定向。

    实现301重定向PHP代码

   
    $the_host = $_SERVER['HTTP_HOST'];  //标记取得当前域名

    $request_url = isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:'';  //判断地址后面部分

    if($the_host !== ‘www.bkjia.com’)  //没有更换的域名地址

    { header('HTTP/1.1 301 Moved Permanently');  // 提示发出301

      header('Location: http://www.bkjia.com/'.$request_url);  //添加新的域名地址

    }

    ?>

    在哪个文件里添加301重定向指令

    在.htaccess文件中增加301重定向指令,采用“mod_rewrite”技术。

    举例:

    RewriteEngine on

    RewriteRule ^(.*)$ http://www.bkjia.com/$1 [R=301,L]

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

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment