[PHP]DEDECMS全站偽靜態詳細教程(首頁、列表、文章頁)

老朱教授發表於2017-10-01

本人比較懶,相信不少朋友也是,懶得每次去生成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




本文轉自黃聰部落格園部落格,原文連結:http://www.cnblogs.com/huangcong/archive/2011/04/22/2025027.html,如需轉載請自行聯絡原作者


相關文章