ecshop是一個很不錯的網店系統,現在很多公司都在用它,它本身優化也很好,不過因為是網店,很多東西都是動態的,所以,對優化來說,不怎么好,不過慶幸的是它可以偽靜態。并且有兩種重寫方法,在后臺的商店設置中,可以選擇簡單重寫和復雜重寫。
偽靜態已經基本上可以滿足大部分人的需求,如果不滿足的還可以根據前面的一篇文章對重寫規則進行修改,以滿足自己的需求。
但是本文所要描述的是,根據ECSHOP內在的一些代碼,我們生成純靜態的網頁,使系統更好的優化。
在這里,我們先對首頁進行純靜態生成。
ECSHOP是一套很好的PHP開源商城系統,但開源產品總是無法符合各個項目的細節需求。負責人要求每個頻道頁都靜態化,以更好的收錄,那我就知道利用dedecms建設一系列的封面模板,然后手動去控制產品的縮略圖,至于產品列表頁,暫時還沒靜態化,還在考慮怎么靜態化比較好,而商城產品內頁靜態化則使用以下代碼。
Php代碼
if(file_exists($htmlfile) && (!$updatehtml)){
header("HTTP/1.1 301 Moved Permanently");
header("Location: {$htmlfile}");
}else{
$htmlcontent = $smarty->make_html("goods.dwt",$cache_id);
if(file_put_contents($htmlfile,$htmlcontent)){
header("HTTP/1.1 301 Moved Permanently");
header("Location: {$htmlfile}");
}
}
if(file_exists($htmlfile) && (!$updatehtml)){ header("HTTP/1.1 301 Moved Permanently"); header("Location: {$htmlfile}"); }else{ $htmlcontent = $smarty->make_html("goods.dwt",$cache_id); if(file_put_contents($htmlfile,$htmlcontent)){ header("HTTP/1.1 301 Moved Permanently"); header("Location: {$htmlfile}"); } }
301轉向是否能夠將收錄的地址改變,這個經過實驗是可以的,大家可以site一下我的商城就知道。其實這個靜態化方法,我的靈感也是來源于supersite,這套開源系統也是經過動態跳轉到靜態化,收錄還不差,所以就模仿著寫。
1.在首頁中,$smarty->display('index.dwt', $cache_id);有這一句,說明是把網頁顯示出來,現在我們把它改成如下代碼(參看注釋)
$file = 'index.html';//靜態網頁文件名
$content = $smarty->make_html('index.dwt');//根據index.dwt模板生成網頁內容
$filename = ROOT_PATH . $file;//靜態網頁路徑
file_put_contents($filename, $content);//生成文件
echo $content;//輸出到頁面
這幾句放在if (!$smarty->is_cached('index.dwt', $cache_id))判斷中這樣可以利用原有的判斷來決定是不是重新生成靜態頁面(不過測試了下是一直重新生成的 這個問題有待繼續研究)
//在判斷外加上
//echo file_get_contents(ROOT_PATH . 'index.html');//輸出靜態頁面
以上幾條簡單的語句,我們就可以生成首頁的靜態網頁。同理,我們可以生成產品類別和產品的靜態網頁,整個系統的靜態化就完成了。
首頁靜態頁面生成后,我們接下來要生成的是產品類別的靜態頁面,我的想法是把產品類別頁面保存在跟目錄下,這樣雖然會比較亂,
但是比較適合優化,因為一般搜索引擎抓取的時候只抓取二到三層。把產品類別放在根目錄,體現產品類別的重要性,易于搜索引擎的
抓取,另外一方面,我們可以把產品放在下個目錄中。
類似代碼:
$filename = build_uri('category', array('cid' => $catinfo['cat_id']));//構造路徑,這個可以選擇自己喜歡的構造方法
$content = $GLOBALS['smarty']->make_html('category.dwt');//產生靜態頁面內容
$filename = ROOT_PATH . $filename;//生成文件路徑,在根目錄下
file_put_contents($filename, $content);//輸出
產品的靜態頁面代碼:
$goodinfo = get_all_goodsinfo($goods_id);
$cat_name = $goodinfo['cat_name'];
$goodsfile = build_uri('goods', array('gid' => $goods_id));
$content = $GLOBALS['smarty']->make_html('goods.dwt');
$html_tempdir = (ROOT_PATH.$cat_name.'/');
if (!is_dir($html_tempdir))//生成產品目錄
{
mkdir($html_tempdir);
}
$htmlfilename = ROOT_PATH . $goodsfile;
file_put_contents($htmlfilename,$content);
我的是使用類alias.html' target='_blank'>別名稱加下劃線:
function build_uri(........)
................
case 'category':
$cat_name = $GLOBALS['db']->getOne('SELECT cat_name FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cid'");
$uri = $cat_name . '-' . $cid;
if (!empty($page))
{
$uri .= '-' . $page;
}
........
case 'goods':
$goods_info = $GLOBALS['db']->getRow('SELECT g.goods_name, c.cat_name FROM ' . $GLOBALS['ecs']->table('goods') . " as g left join " .
$GLOBALS['ecs']->table('category') . " as c on c.cat_id = g.cat_id WHERE g.goods_id = '$gid'");
$goods_name = $goods_info['goods_name'];
$cat_name = $cat_name;
$uri = $cat_name . '/' . $goods_name . '-' . $gid ;
..........................
有人問 make_html 這個函數在那里: 我現在補充如下:
在 includes 下的 cls_template.php 加上
function make_html($filename, $cache_id = '')
{
ob_start();
$this->display($filename,$cache_id);