Home > php教程 > php手册 > body text

PHP实现301永久重定向方法

WBOY
Release: 2016-05-25 16:47:27
Original
1069 people have browsed it

本文章来介绍一下PHP实现301永久重定向将不带www的域名指向带www的域名,实现方法得出.

当你的站点域名发生变化或者网页地址更改(网页更换存放目录或者网页文件的名称发生改变),如何确保站点在过渡交接期内来自搜索引擎的流量不会丢失,同时可以更新搜索引擎结果中的网页地址?301重定向是解决这个问题的理想方法,它可以把用户和搜索引擎定向至正确的网页

实例代码如下:

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

1.在.htaccess文件中增加301重定向指令采用"mod_rewrite"技术,形如:

实例代码如下:

RewriteEngine on  
RewriteRule ^(.*)$ http://www.phprm.com/$1 [R=301,L]
Copy after login


Related labels:
php
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!