Home > php教程 > php手册 > PHP教程:如何实现301重定向

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 11:33:59
Original
934 people have browsed it

什么是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]

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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template