ecshop自定义目录结构伪静态url

2013-03-27 23:18 来源:www.chinab4c.com 作者:ecshop专家

    ecshop自定义目录结构伪静态url,这个功能是非常多的人想要的,其实很简单,。就是在ecshop后台商品编辑录入的时候。或者是ecshop商品分类录入编辑的时候,可以自定义目录结构,让自己的商品地址url以及分类地址url看上去,更加的个性化。下面我们将结合ecshop的数据库处理以及php程序处理,来完成ecshop自定义目录结构伪静态url。

  1:修改ecshop商品表ecs_goods,ecs_category的 ecshop数据库结构,增加url 字段

   alter table ecs_goods add column url varchar(128) not null default '';

   alter table ecs_category add column url varchar(128) not null default '';

 2:admin/goods.php修改以下代码

   $sql = "INSERT INTO " . $ecs->table('goods') . " (goods_name, goods_name_style, goods_sn, " .
                    "cat_id, brand_id, shop_price, market_price, is_promote, promote_price, " .
                    "promote_start_date, promote_end_date, goods_img, goods_thumb, original_img, keywords, goods_brief, " .
                    "seller_note, goods_weight, goods_number, warn_number, integral, give_integral, is_best, is_new, is_hot, " .
                    "is_on_sale, is_alone_sale, is_shipping, goods_desc, add_time, last_update, goods_type, rank_integral, suppliers_id,url)" .
                "VALUES ('$_POST[goods_name]', '$goods_name_style', '$goods_sn', '$catgory_id', " .
                    "'$brand_id', '$shop_price', '$market_price', '$is_promote','$promote_price', ".
                    "'$promote_start_date', '$promote_end_date', '$goods_img', '$goods_thumb', '$original_img', ".
                    "'$_POST[keywords]', '$_POST[goods_brief]', '$_POST[seller_note]', '$goods_weight', '$goods_number',".
                    " '$warn_number', '$_POST[integral]', '$give_integral', '$is_best', '$is_new', '$is_hot', '$is_on_sale', '$is_alone_sale', $is_shipping, ".
                    " '$_POST[goods_desc]', '" . gmtime() . "', '". gmtime() ."', '$goods_type', '$rank_integral', '$suppliers_id','$_POST[url]')";

 

 3:admin/category.php修改以下代码

    $cat['url'] = !empty($_POST['url']) ? trim($_POST['url']) : '';

  4:调整url规则,以下是apache下面的规则

    RewriteRule ^(.*?)\/([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$  category\.php\?id=$2&brand=$3&price_min=$4&price_max=$5&filter_attr=$6&page=$7&sort=$8&order=$9 [QSA,L]
RewriteRule ^(.*?)\/([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)(.*)\.html$                            category\.php\?id=$2&brand=$3&price_min=$4&price_max=$5&filter_attr=$6 [QSA,L]
RewriteRule ^(.*?)\/([0-9]+)-b([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$                                      category\.php\?id=$2&brand=$3&page=$4&sort=$5&order=$6 [QSA,L]
RewriteRule ^(.*?)\/([0-9]+)-b([0-9]+)-([0-9]+)(.*)\.html$                                                       category\.php\?id=$2&brand=$3&page=$4  [QSA,L]
RewriteRule ^(.*?)\/([0-9]+)-b([0-9]+)(.*)\.html$                                                                category\.php\?id=$2&brand=$3  [QSA,L]
RewriteRule ^(.*?)\/([0-9]+)(.*)\.html$                                                                          category\.php\?id=$2  [QSA,L]

RewriteRule ^(.*?)\/([0-9]+)$  goods\.php\?id=$2 [QSA,L]

  5:includes/lib_common.php的build_uri函数修改以下几点

  function build_uri($app, $params, $append = '', $page = 0, $keywords = '', $size = 0)
{
    static $rewrite = NULL;
    global $db;
    global $ecs;
    if ($rewrite === NULL)
    {
        $rewrite = intval($GLOBALS['_CFG']['rewrite']);
    }

    $args = array('cid'   => 0,
                  'gid'   => 0,
                  'bid'   => 0,
                  'acid'  => 0,
                  'aid'   => 0,
                  'sid'   => 0,
                  'gbid'  => 0,
                  'auid'  => 0,
                  'sort'  => '',
                  'order' => '',
                );

    extract(array_merge($args, $params));

    $uri = '';
    switch ($app)
    {
        case 'category':
            if (empty($cid))
            {
                return false;
            }
            else
            {
                if ($rewrite)
                {    
                    $url = $db->getOne("select url from ".$ecs->table('category')." where cat_id = '$cid'");
                   
                    if($url){
                        $uri = $url . $cid;
                    }else{
                        $uri = '/category/' . $cid;
                    }
                    if (isset($bid))
                    {
                        $uri .= '-b' . $bid;
                    }
                    if (isset($price_min))
                    {
                        $uri .= '-min'.$price_min;
                    }
                    if (isset($price_max))
                    {
                        $uri .= '-max'.$price_max;
                    }
                    if (isset($filter_attr))
                    {
                        $uri .= '-attr' . $filter_attr;
                    }
                    if (!empty($page))
                    {
                        $uri .= '-' . $page;
                    }
                    if (!empty($sort))
                    {
                        $uri .= '-' . $sort;
                    }
                    if (!empty($order))
                    {
                        $uri .= '-' . $order;
                    }
                }
                else
                {
                    $uri = '/category.php?id=' . $cid;
                    if (!empty($bid))
                    {
                        $uri .= '&brand=' . $bid;
                    }
                    if (isset($price_min))
                    {
                        $uri .= '&price_min=' . $price_min;
                    }
                    if (isset($price_max))
                    {
                        $uri .= '&price_max=' . $price_max;
                    }
                    if (!empty($filter_attr))
                    {
                        $uri .='&filter_attr=' . $filter_attr;
                    }

                    if (!empty($page))
                    {
                        $uri .= '&page=' . $page;
                    }
                    if (!empty($sort))
                    {
                        $uri .= '&sort=' . $sort;
                    }
                    if (!empty($order))
                    {
                        $uri .= '&order=' . $order;
                    }
                }
            }

            break;
        case 'goods':
            if (empty($gid))
            {
                return false;
            }
            else
            {    
                $url = $db->getOne("select url from ".$ecs->table('goods')." where goods_id = '$gid'");
                if(empty($url)){
                    $url = '/goods/' . $gid;
                }
                $uri = $rewrite ? $url . $gid : '/goods.php?id=' . $gid;
            }

            break;
        case 'brand':
            if (empty($bid))
            {
                return false;
            }
            else
            {
                if ($rewrite)
                {
                    $uri = '/brand-' . $bid;
                    if (isset($cid))
                    {
                        $uri .= '-c' . $cid;
                    }
                    if (!empty($page))
                    {
                        $uri .= '-' . $page;
                    }
                    if (!empty($sort))
                    {
                        $uri .= '-' . $sort;
                    }
                    if (!empty($order))
                    {
                        $uri .= '-' . $order;
                    }
                }
                else
                {
                    $uri = '/brand.php?id=' . $bid;
                    if (!empty($cid))
                    {
                        $uri .= '&cat=' . $cid;
                    }
                    if (!empty($page))
                    {
                        $uri .= '&page=' . $page;
                    }
                    if (!empty($sort))
                    {
                        $uri .= '&sort=' . $sort;
                    }
                    if (!empty($order))
                    {
                        $uri .= '&order=' . $order;
                    }
                }
            }

            break;
        case 'article_cat':
            if (empty($acid))
            {
                return false;
            }
            else
            {
                if ($rewrite)
                {
                    $uri = '/article_cat-' . $acid;
                    if (!empty($page))
                    {
                        $uri .= '-' . $page;
                    }
                    if (!empty($sort))
                    {
                        $uri .= '-' . $sort;
                    }
                    if (!empty($order))
                    {
                        $uri .= '-' . $order;
                    }
                    if (!empty($keywords))
                    {
                        $uri .= '-' . $keywords;
                    }
                }
                else
                {
                    $uri = '/article_cat.php?id=' . $acid;
                    if (!empty($page))
                    {
                        $uri .= '&page=' . $page;
                    }
                    if (!empty($sort))
                    {
                        $uri .= '&sort=' . $sort;
                    }
                    if (!empty($order))
                    {
                        $uri .= '&order=' . $order;
                    }
                    if (!empty($keywords))
                    {
                        $uri .= '&keywords=' . $keywords;
                    }
                }
            }

            break;
        case 'article':
            if (empty($aid))
            {
                return false;
            }
            else
            {
                $uri = $rewrite ? '/article-' . $aid : '/article.php?id=' . $aid;
            }

            break;
        case 'group_buy':
            if (empty($gbid))
            {
                return false;
            }
            else
            {
                $uri = $rewrite ? '/group_buy-' . $gbid : '/group_buy.php?act=view&id=' . $gbid;
            }

            break;
        case 'auction':
            if (empty($auid))
            {
                return false;
            }
            else
            {
                $uri = $rewrite ? '/auction-' . $auid : '/auction.php?act=view&id=' . $auid;
            }

            break;
        case 'snatch':
            if (empty($sid))
            {
                return false;
            }
            else
            {
                $uri = $rewrite ? '/snatch-' . $sid : '/snatch.php?id=' . $sid;
            }

            break;
        case 'search':
            break;
        case 'exchange':
            if ($rewrite)
            {
                $uri = '/exchange-' . $cid;
                if (isset($price_min))
                {
                    $uri .= '-min'.$price_min;
                }
                if (isset($price_max))
                {
                    $uri .= '-max'.$price_max;
                }
                if (!empty($page))
                {
                    $uri .= '-' . $page;
                }
                if (!empty($sort))
                {
                    $uri .= '-' . $sort;
                }
                if (!empty($order))
                {
                    $uri .= '-' . $order;
                }
            }
            else
            {
                $uri = '/exchange.php?cat_id=' . $cid;
                if (isset($price_min))
                {
                    $uri .= '&integral_min=' . $price_min;
                }
                if (isset($price_max))
                {
                    $uri .= '&integral_max=' . $price_max;
                }

                if (!empty($page))
                {
                    $uri .= '&page=' . $page;
                }
                if (!empty($sort))
                {
                    $uri .= '&sort=' . $sort;
                }
                if (!empty($order))
                {
                    $uri .= '&order=' . $order;
                }
            }

            break;
        case 'exchange_goods':
            if (empty($gid))
            {
                return false;
            }
            else
            {
                $uri = $rewrite ? '/exchange-id' . $gid : '/exchange.php?id=' . $gid . '&act=view';
            }

            break;
        default:
            return false;
            break;
    }

    if ($rewrite)
    {
        if ($rewrite == 2 && !empty($append))
        {
            $uri .= '-' . urlencode(preg_replace('/[\.|\/|\?|&|\+|\\\|\'|"|,]+/', '', $append));
        }
        if($app !=='goods'){
            $uri .= '.html';
        }
        
    }
    if (($rewrite == 2) && (strpos(strtolower(EC_CHARSET), 'utf') !== 0))
    {
        $uri = urlencode($uri);
    }
    return $uri;
}

 6:includes/init.php里面修改成以下

   if (!empty($_CFG['stylename']))
    {
        $smarty->assign('ecs_css_path', '/themes/' . $_CFG['template'] . '/style_' . $_CFG['stylename'] . '.css');
    }
    else
    {
        $smarty->assign('ecs_css_path', '/themes/' . $_CFG['template'] . '/style.css');
    }
 7:所有模板的js,使用/js/commmon.js格式,所有的js目录下的ajax请求的php前面。都使用/相对路径。比如/user.php

 8:模板的img路径。可以使用/相对路径来处理

    $tmp_dir   = '/themes/' . $GLOBALS['_CFG']['template'] . '/'; // 模板所在路径

    这样就完成了ecshop自定义目录结构伪静态url修改。

    相关文章:http://www.chinab4c.com/html/chajian/ecshopchajian/201111/07-917.html

    来源:http://www.chinab4c.com