黄聪:[PHP]DEDECMS全站伪静态详细教程(首页、列表、文章页)

本人比较懒,相信不少朋友也是,懒得每次去生成dedecms的HTML,同时为了现已不太有效的伪静态优化,所以还是搞搞伪静态吧。

dedecms全站伪静态教程分为五部分,包括“Apache设置、.htaccess文件设置、首页伪静态、列表页伪静态、文章页伪静态”。

用4.0测试OK,5.X没测。

一、Apache文件设置

这是重头戏,以前用本地测试时因 httpd.conf 文件没设置好,搞得焦头烂额。

1、如果是虚拟主机,一般 apache 的 LoadModule rewrite_module modules/mod_rewrite.so 是开启的,请咨询你的主机提供商以确认是否支持伪静态。

2、如果是自己的服务器或本地机器,请按以下设置:

关闭 apache 服务。

打开 httpd.conf 文件(一般放在apache安装目录的conf目录下)把 #LoadModule rewrite_module modules/mod_rewrite.so 前的 # 号去掉,代表开启 rewrite 规则。

搜索 “AllowOverride None”(不包括引号,下同) ,有多个,全部替换为 “AllowOverride All”。

开启 apache 服务。

二、.htaccess文件设置

在网站根目录建一 .htaccess 文件,内容为:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)index\.html$ $1/index.php
RewriteRule ^(.*)list-([0-9]+)\.html$ $1/plus/list.php?tid=$2
RewriteRule ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1/plus/list.php?typeid=$2&PageNo=$3
RewriteRule ^(.*)view-([0-9]+).html$ $1/plus/view.php?aid=$2
RewriteRule ^(.*)view-([0-9]+)-([0-9]+).html$ $1/plus/view.php?aid=$2&pageno=$3

三、dedecms首页伪静态

不要更新首页html就行。如果已更新,请删除index.html即可。

四、dedecms列表页伪静态

1、打开 /include/inc_arclist_view.php 找到

//---------------------------------
//获取动态的分页列表
//---------------------------------

把它下面的函数改成以下的:

function GetPageListDM($list_len,$listitem="index,end,pre,next,pageno")
    {
        $prepage="";
        $nextpage="";
        $prepagenum = $this->PageNo-1;
        $nextpagenum = $this->PageNo+1;
        if($list_len==""||ereg("[^0-9]",$list_len)) $list_len=3;
        $totalpage = ceil($this->TotalResult/$this->PageSize);
        if($totalpage<=1 && $this->TotalResult>0) return "共1页/".$this->TotalResult."条记录";
        if($this->TotalResult == 0) return "共0页".$this->TotalResult."条记录";
        $maininfo = "共{$totalpage}页/".$this->TotalResult."条记录";
       
        $purl = $this->GetCurUrl();
        $geturl = "typeid=".$this->TypeID."&TotalResult=".$this->TotalResult."&";
        $gettypeid =$this->TypeID."-";
        //$hidenform = "<input type='hidden' name='typeid' value='".$this->TypeID."'>";
        //$hidenform .= "<input type='hidden' name='TotalResult' value='".$this->TotalResult."'>";
       
        $purl .= "?".$geturl;
       
        //获得上一页和下一页的链接
        if($this->PageNo != 1){
            $prepage.="<a href='list-".$gettypeid."$prepagenum.html'>上一页</a>";
            $indexpage="<a href='list-".$gettypeid."1.html'>首页</a>";
        }
        else{
            $indexpage="首页";
        }   
       
        if($this->PageNo!=$totalpage && $totalpage>1){
            $nextpage.="<a href='list-".$gettypeid."$nextpagenum.html'>下一页</a>";
            $endpage="<a href='list-".$gettypeid."$totalpage.html'>末页</a>";
        }
        else{
            $endpage="末页";
        }
        //获得数字链接
        $listdd="";
        $total_list = $list_len * 2 + 1;
        if($this->PageNo >= $total_list) {
            $j = $this->PageNo-$list_len;
            $total_list = $this->PageNo+$list_len;
            if($total_list>$totalpage) $total_list=$totalpage;
        }else{
              $j=1;
              if($total_list>$totalpage) $total_list=$totalpage;
        }
        for($j;$j<=$total_list;$j++){
          $listdd.="<a href='list-".$gettypeid."$j.html'>".$j."</a>";
        }
        $plist .= $maininfo.$indexpage.$prepage.$listdd.$nextpage.$endpage;
        return $plist;
    }

2、打开 /include/inc_channel_unit_functions.php 查找

    $reurl = $GLOBALS["cfg_plus_dir"]."/list.php?tid=".$typeid;

改成

    $reurl = "/list-".$typeid.".html"; //列表页伪静态

五、dedecms文章页伪静态

1、打开 /include/inc_archives_view.php ,有两处修改的地方,

1.1 找到:

        //------------------------
         //获得动态页面分页列表
         //------------------------
   function GetPagebreakDM($totalPage,$nowPage,$aid)
        {
                if($totalPage==1){ return ""; }
                $PageList = "共".$totalPage."页: ";
                $nPage = $nowPage-1;
                $lPage = $nowPage+1;
                if($nowPage==1) $PageList.="上一页 ";
                else{
                  if($nPage==1) $PageList.="<a href='view.php?aid=$aid'>上一页</a> ";
                  else $PageList.="<a href='view.php?aid=$aid&pageno=$nPage'>上一页</a> ";
                }
                for($i=1;$i<=$totalPage;$i++)
                {
                        if($i==1){
                          if($nowPage!=1) $PageList.="<a href='view.php?aid=$aid'>[1]</a> ";
                          else $PageList.="1 ";
                        }else{
                          $n = $i;
                          if($nowPage!=$i) $PageList.="<a href='view.php?aid=$aid&pageno=$i'>[".$n."]</a> ";
                          else $PageList.="$n ";
                        }
                }
                if($lPage <= $totalPage) $PageList.="<a href='view.php?aid=$aid&pageno=$lPage'>下一页</a> ";
                else $PageList.= "下一页 ";
                return $PageList;
        }

修改为:

//获得伪静态页面分页列表
   function GetPagebreakDM($totalPage,$nowPage,$aid)
        {
                if($totalPage==1){ return ""; }
                $PageList = "共".$totalPage."页: ";
                $nPage = $nowPage-1;
                $lPage = $nowPage+1;
                if($nowPage==1) $PageList.="上一页 ";
                else{
                  if($nPage==1) $PageList.="<a href='view-$aid'>上一页</a> ";
                  else $PageList.="<a href='view-$aid-$nPage.html'>上一页</a> ";
                }
                for($i=1;$i<=$totalPage;$i++)
                {
                        if($i==1){
                          if($nowPage!=1) $PageList.="<a href='view-$aid'>[1]</a> ";
                          else $PageList.="1 ";
                        }else{
                          $n = $i;
                          if($nowPage!=$i) $PageList.="<a href='view-$aid-$i.html'>[".$n."]</a> ";
                          else $PageList.="$n ";
                        }
                }
                if($lPage <= $totalPage) $PageList.="<a href='view-$aid-$lPage.html'>下一页</a> ";
                else $PageList.= "下一页 ";
                return $PageList;
        }  

1.2 找到:

        //-------------------------
        //获得动态页面小标题
        //-------------------------
   function GetPageTitlesDM($styleName,$pageNo)
        {
                if($this->TotalPage==1){ return ""; }
                if(count($this->SplitTitles)==0){ return ""; }
                $i=1;
                $aid = $this->ArcID;
                if($styleName=='link')
                {
                        $revalue = "";
                  foreach($this->SplitTitles as $k=>$v){
                           if($i==1) $revalue .= "<a href='view.php?aid=$aid&pageno=$i'>$v</a> ";
                     else{
                              if($pageNo==$i) $revalue .= " $v ";
                              else $revalue .= "<a href='view.php?aid=$aid&pageno=$i'>$v</a> ";
                     }
                     $i++;
                  }
          }else
          {
                  $revalue = "<select id='dedepagetitles' onchange='location.href=this.options[this.selectedIndex].value;'>";
                        foreach($this->SplitTitles as $k=>$v){
                           if($i==1) $revalue .= "<option value='".$this->Fields['phpurl']."/view.php?aid=$aid&pageno=$i'& gt;{$i}、{$v}</option>";
                     else{
                              if($pageNo==$i) $revalue .= "<option value='".$this->Fields['phpurl']."/view.php?aid=$aid&pageno=$i' selected>{$i}、{$v}</option>";
                              else $revalue .= "<option value='".$this->Fields['phpurl']."/view.php?aid=$aid&pageno=$i'& gt;{$i}、{$v}</option>";
                     }
                     $i++;
                  }
                  $revalue .= "</select>";
          }
                return $revalue;
        }

修改为:

   //获取伪静态页面小标题
   function GetPageTitlesDM($styleName,$pageNo)
        {
                if($this->TotalPage==1){ return ""; }
                if(count($this->SplitTitles)==0){ return ""; }
                $i=1;
                $aid = $this->ArcID;
                if($styleName=='link')
                {
                        $revalue = "";
                  foreach($this->SplitTitles as $k=>$v){
                           if($i==1) $revalue .= "<a href='view-$aid-$i.html'>$v</a> ";
                     else{
                              if($pageNo==$i) $revalue .= " $v ";
                              else $revalue .= "<a href='view-$aid-$i.html'>$v</a> ";
                     }
                     $i++;
                  }
          }else
          {
                  $revalue = "<select id='dedepagetitles' onchange='location.href=this.options[this.selectedIndex].value;'>";
                        foreach($this->SplitTitles as $k=>$v){
                           if($i==1) $revalue .= "<option value='".$this->Fields['phpurl']."/view-$aid-$i.html'>{$i}、{$v}& lt;/option>";
                     else{
                              if($pageNo==$i) $revalue .= "<option value='".$this->Fields['phpurl']."/view-$aid-$i.html' selected>{$i}、{$v}</option>";
                              else $revalue .= "<option value='".$this->Fields['phpurl']."/view-$aid-$i'>{$i}、{$v}< /option>";
                     }
                     $i++;
                  }
                  $revalue .= "</select>";
          }
                return $revalue;
        }  


2、打开 /include/inc_channel_unit_functions.php 查找

    return $GLOBALS['cfg_plus_dir']."/view.php?aid=$aid";

改成

    return "/view-$aid.html";//文章页伪静态

六、完工。把栏目和文章都改为动态,刷新,dedecms全站伪静态成功。

把栏目和文章批量修改为动态的SQL语句:

update dede_arctype set isdefault=-1;

update dede_archives set ismake=-1;

更新:

DEDECMS搜索的伪静态在 .htaccess 文件里加入如下两句即可:

RewriteRule ^(.*)/search/([^/]*)$   $1/plus/search\.php\?keyword=$2
RewriteRule ^(.*)/search/([^/]*)/(\d)$   $1/plus/search\.php\?keyword=$2&PageNo=$3

你可能感兴趣的:(dedecms)