建站教程

      織夢dedecms5.7sp2標簽TAG偽靜態分頁設置和Linux的rewrite規則

      2021-02-24
      0
      織夢DEDECMS有比較強大的TAG功能,但是它的標簽TAG功能較差,不利于seo優化,同時也有很多問題,比如:當前頁不存在上一頁時,鏈接為“-1”的問題,還有出現“系統無此標簽,可能已經移除”的問題。夢飛在網上找了好多教程,但都無法成功實現在LINUX環境下的rewrite規則標簽分頁偽靜態,特安裝了CentOS7系統,在LAMP系統PHP7環境下,用DEDECMS 5.7sp2測試出正確的方法,把標簽偽靜態分頁方法分享給大家(部分資料來源于網絡),同時也修復了一些上述提到的BUG。
      為節約各位站長時間,可以先看夢飛的演示網站 http://www.fij777.cn/tags.php 是不是自己想要的效果,再往下看,謝謝。
       
      1.修改織夢前臺顯示鏈接
      這里達到的效果就是使原來/tags.php?keywors更改為/tags/keywords.html。
      這里主要修改下調用的標簽,在/include/taglib/tag.lib.php中,在87行找到
      $row['link'] = $cfg_cmsurl."/tags?".urlencode($row['keyword']);
      將其修改為:
      $row['link'] = $cfg_cmsurl."/tags/".urlencode($row['keyword']).".html";
       
      2.修改分頁代碼部分
      這部分修改是關鍵,一是解決bug,二是和rewrite配套實現dedecms分頁偽靜態顯示。
      我們需要修改include/arc.taglist.class.php,找到分頁函數,大概位于429行,您可以細心修改,也可以直接將其替換為:
      /**
       *  獲取動態的分頁列表
       *
       * @access    public
       * @param     int  $list_len  列表寬度
       * @param     string  $listitem  列表樣式
       * @return    string
       */
      function GetPageListDM($list_len,$listitem="info,index,end,pre,next,pageno")
      {
      $prepage="";
      $nextpage="";
      $prepagenum = $this->PageNo - 1;
      $nextpagenum = $this->PageNo + 1;
      if($list_len == "" || preg_match("/[^0-9]/", $list_len))
      {
      $list_len = 3;
      }
      $totalpage = $this->TotalPage;
      if($totalpage <= 1 && $this->TotalResult > 0)
      {
      return "<span class="pageinfo">共1頁/".$this->TotalResult."條</span>";
      }
      if($this->TotalResult == 0)
      {
      return "<span class="pageinfo">共0頁/".$this->TotalResult."條</span>";
      }
      $maininfo = "<span class="pageinfo">共{$totalpage}頁/".$this->TotalResult."條</span> ";
      $purl = $this->GetCurUrl();
      $basename = basename($purl); 
      $tmpname = explode('.', $basename); 
      $purl = str_replace($basename, '', $purl).urlencode($this->Tag); 
      //var_dump($purl);exit; 
      //$purl .= "?/".urlencode($this->Tag);
       
      //獲得上一頁和下一頁的鏈接
      //if($this->PageNo != 1) 這是修正上一頁為負數的問題,夢飛,2021-02-25
      if($this->PageNo != 1 && $this->PageNo != "")
       
      {
      $prepage.="<li><a href='".$purl."-$prepagenum.html'>上一頁</a></li> ";  //注意這里的修改,夢飛,2021-02-25
      $indexpage="<li><a href='".$purl."-1.html'>首頁</a></li> ";  //注意這里的修改,夢飛,2021-02-25
      }
      else
      {
      $indexpage="<li><a>首頁</a></li> ";
      }
      if($this->PageNo!=$totalpage && $totalpage>1)
      {
      $nextpage.="<li><a href='".$purl."-$nextpagenum.html'>下一頁</a></li> ";  //注意這里的修改,夢飛,2021-02-25
      $endpage="<li><a href='".$purl."-$totalpage.html'>末頁</a></li> ";  //注意這里的修改,夢飛,2021-02-25
      }
      else
      {
      $endpage="<li><a>末頁</a></li> ";
      }
       
      //獲得數字鏈接
      $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++)
      {
      if($j == $this->PageNo)
      {
      $listdd.= "<li class="thisclass"><a>$j</a></li> ";
      }
      else
      {
      $listdd.="<li><a href='".$purl."-$j.html'>".$j."</a></li> ";  //注意這里的修改,夢飛,2021-02-25
      }
      }
      $plist  =  '';
      if(preg_match('/info/i', $listitem))
      {
      $plist .= $maininfo.' ';
      }
      if(preg_match('/index/i', $listitem))
      {
      $plist .= $indexpage.' ';
      }
      if(preg_match('/pre/i', $listitem))
      {
      $plist .= $prepage.' ';
      }
      if(preg_match('/pageno/i', $listitem))
      {
      $plist .= $listdd.' ';
      }
      if(preg_match('/next/i', $listitem))
      {
      $plist .= $nextpage.' ';
      }
      if(preg_match('/end/i', $listitem))
      {
      $plist .= $endpage.' ';
      }
      return $plist;
      }
      3.設置偽靜態規則
      注意這個偽靜態一定要和上面的代碼配套,不要把網上的教程里面的方法混合使用,那樣多半不成功
      我們這里以CentOS7和dedecms 5.7為例子,設置.htaccess以下規則:
      RewriteEngine On
      RewriteBase /
      RewriteRule tags/([^-]+).html$ /tags.php?/$1
      RewriteRule tags/([^-]+)-([0-9]+).html$ /tags.php?/$1/$2
       
      如果是iis7,設置以下規則:
      <?xml version="1.0" encoding="UTF-8"?> 
      <configuration> 
      <system.webServer> 
      <rewrite> 
      <rules> 
      <rule name="weather1" stopProcessing="true"> 
      <match url="tags/([^-]+).html$" ignoreCase="true" /> 
      <conditions logicalGrouping="MatchAll"> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
      </conditions> 
      <action type="Rewrite" url="/tags.php?/{R:1}" appendQueryString="false" /> 
      </rule> 
      <rule name="weather2" stopProcessing="true"> 
      <match url="tags/([^-]+)-([0-9]+).html$" ignoreCase="true" /> 
      <conditions logicalGrouping="MatchAll"> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
      </conditions> 
      <action type="Rewrite" url="/tags.php?/{R:1}/{R:2}" appendQueryString="false" /> 
      </rule> 
      </rules> 
      </rewrite> 
      </system.webServer> 
      </configuration> 
       
      4、好了,至此就搞定了。演示可以看 http://www.fij777.cn/tags.php 南華中天官網,謝謝。
       
      部分文章來源與網絡,若有侵權請聯系站長刪除!

      推薦產品