首页 > 建站教程 > CMS教程 > dedecms >  dedecms的301跳转,织梦301跳转,php 301跳转正文

dedecms的301跳转,织梦301跳转,php 301跳转

dedecms 301跳转

首先声明下,本dedecms的301跳转教程仅适用网站首页为php后缀的dedecms网站,如果首页生成为静态不行。因为浏览时不经过php文件(经测试,没有任何问题,本站用的就是这种方法)。

第一步:打开网站根目录的index.php文件(记得备份),拷贝下面代码,覆盖原来内容:


01<?php
02/**
03 * @version        $Id: index.php 1 9:23 2010-11-11 tianya $
04 * @package        DedeCMS.Site
05 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
07 * @link           http://www.dedecms.com
08 */
09  
10//主域名301跳转到www 
11$redirect301=1; //301跳转开关,1代表打开,0代表关闭 
12$index_file='index.html'; //指定网站默认首页文件,DeDeCMS设置为index.html,不支持SSI(shtml/shtm) 
13if(substr($_SERVER['SERVER_NAME'],0,4)!='www.'&&$redirect301) //判断URL中是否带www 
14
15header('HTTP/1.1 301 Moved Permanently'); 
16header('Location:http://www.'.$_SERVER['SERVER_NAME']); //301跳转到www 
17exit(); 
18}
19 
20//自动生成HTML版
21if(isset($_GET['upcache']) || !file_exists('index.html'))
22{
23    require_once (dirname(__FILE__) . "/include/common.inc.php");
24    require_once DEDEINC."/arc.partview.class.php";
25    $GLOBALS['_arclistEnv'] = 'index';
26    $row = $dsql->GetOne("Select * From `dede_homepageset`");
27    $row['templet'] = MfTemplet($row['templet']);
28    $pv = new PartView();
29    $pv->SetTemplet($cfg_basedir . $cfg_templets_dir . "/" . $row['templet']);
30    $row['showmod'] = isset($row['showmod'])? $row['showmod'] : 0;
31    if ($row['showmod'] == 1)
32    {
33        $pv->SaveToHtml(dirname(__FILE__).'/index.html');
34        include(dirname(__FILE__).'/index.html');
35        exit();
36    } else {
37        $pv->Display();
38        exit();
39    }
40}
41else
42{
43    header('HTTP/1.1 301 Moved Permanently');
44    header('Location:index.html');
45}
46?>
第二步:上传到根目录。


第三步:进入后台-->生成-->更新主页html-->勾选“动态浏览”,或者直接删除根目录的index.html文件

第四步:进入主机管理,找到“默认首页设置”,把index.php提到最前面。如下图:

网站默认首页设置

OK,大功告成,在浏览器输入不带www的域名,看看会不会自动跳转到带www的域名。