首先声明下,本dedecms的301跳转教程仅适用网站首页为php后缀的dedecms网站,如果首页生成为静态不行。因为浏览时不经过php文件(经测试,没有任何问题,本站用的就是这种方法)。
第一步:打开网站根目录的index.php文件(记得备份),拷贝下面代码,覆盖原来内容:
12 | $index_file = 'index.html' ; |
13 | if ( substr ( $_SERVER [ 'SERVER_NAME' ],0,4)!= 'www.' && $redirect301 ) |
15 | header( 'HTTP/1.1 301 Moved Permanently' ); |
16 | header( 'Location:http://www.' . $_SERVER [ 'SERVER_NAME' ]); //301跳转到www |
21 | if (isset( $_GET [ 'upcache' ]) || ! file_exists ( 'index.html' )) |
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' ]); |
29 | $pv ->SetTemplet( $cfg_basedir . $cfg_templets_dir . "/" . $row [ 'templet' ]); |
30 | $row [ 'showmod' ] = isset( $row [ 'showmod' ])? $row [ 'showmod' ] : 0; |
31 | if ( $row [ 'showmod' ] == 1) |
33 | $pv ->SaveToHtml(dirname( __FILE__ ). '/index.html' ); |
34 | include (dirname( __FILE__ ). '/index.html' ); |
43 | header( 'HTTP/1.1 301 Moved Permanently' ); |
44 | header( 'Location:index.html' ); |
第二步:上传到根目录。
第三步:进入后台-->生成-->更新主页html-->勾选“动态浏览”,或者直接删除根目录的index.html文件
第四步:进入主机管理,找到“默认首页设置”,把index.php提到最前面。如下图:
OK,大功告成,在浏览器输入不带www的域名,看看会不会自动跳转到带www的域名。