一聚教程网:一个值得你收藏的教程网站

热门教程

phpcms中文URL路径解决方法

时间:2022-06-25 16:42:49 编辑:袖梨 来源:一聚教程网

php教程cms怎样支持中文路径呢?phpcms中文URL要怎样设置呢?
待我下面叙来。。。
首先,在 phpcms的include目录下,有global.func.php 文件。
找到 function createhtml($file) 这个函数。
说明下,这个函数是用来生成html静态文件的。
我们只需要修改下这个函数即可。
修改后如下:
 
 
view plaincopy to clipboardprint?
  1. function createhtml($file)   
  2. {   
  3.     //scofield 2010-3-16   
  4.     //for create file as chinses name   
  5.     $file = iconv('UTF-8', 'GBK', $file);   
  6.   
  7.     $data = ob_get_contents();   
  8.     ob_clean();   
  9.     dir_create(dirname($file));   
  10.     $strlen = file_put_contents($file, $data);   
  11.     @chmod($file,0777);   
  12.     return $strlen;   
  13. }  
function createhtml($file)
{
	//scofield 2010-3-16
	//for create file as chinses name
	$file = iconv('UTF-8', 'GBK', $file);

	$data = ob_get_contents();
	ob_clean();
	dir_create(dirname($file));
	$strlen = file_put_contents($file, $data);
	@chmod($file,0777);
	return $strlen;
}
 
 
 
$file = iconv(‘UTF-8′, ‘GBK’, $file); 主要增加了这一句。该句实现了 编码转换。这样生成的 html 文件或者路径 都可以带有中文。
 
其次,在include目录下还有 include/url.class.php 这个文件。
打开后找到 function show($contentid, $page = 0, $catid = 0, $time = 0, $prefix = ”) 这个函数。
添加 如下语句
 
 
view plaincopy to clipboardprint?
  1. //scofield 2010-3-16   
  2. //get root category name   
  3. $catetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE  catid = $catid  LIMIT 1");   
  4. $arrparentidarr = explode(',',$catetemp['arrparentid']);   
  5. $rootidtemp = $arrparentidarr[count($arrparentidarr)-1];   
  6. $rootcatetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE  catid = $rootidtemp LIMIT 1");   
  7. $rootcatename = $rootcatetemp['catname'];   
  8. //for url roles add content title and category name   
  9. $showtitle = strip_tags($r['title']);   
  10. $catdirname = $C['catname'];  
//scofield 2010-3-16
//get root category name
$catetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE  catid = $catid  LIMIT 1");
$arrparentidarr = explode(',',$catetemp['arrparentid']);
$rootidtemp = $arrparentidarr[count($arrparentidarr)-1];
$rootcatetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE  catid = $rootidtemp LIMIT 1");
$rootcatename = $rootcatetemp['catname'];
//for url roles add content title and category name
$showtitle = strip_tags($r['title']);
$catdirname = $C['catname'];
 
 
 
完整的函数如下:
 
 
view plaincopy to clipboardprint?
  1. function show($contentid, $page = 0, $catid = 0, $time = 0, $prefix = '')   
  2. {   
  3.     global $PHPCMS;   
  4.     if($catid == 0 || $time == 0 || $prefix == '')   
  5.     {   
  6.         $r = $this->db->get_one("SELECT * FROM `".DB_PRE."content` WHERE `contentid`='$contentid'");   
  7.         if($r['isupgrade'] && !emptyempty($r['url']))   
  8.         {   
  9.             if($page>1)   
  10.             {   
  11.                 $base_name = basename($r['url']);   
  12.                 $fileext = fileext($base_name);   
  13.                 $url_a[0] = $url_a[1] = preg_replace('/.'.$fileext.'$/','_'.$page.'.'.$fileext,$r['url']);   
  14.                 return $url_a;   
  15.             }   
  16.             else  
  17.             {   
  18.                 $url_a[0] = $url_a[1] = $r['url'];   
  19.                 return $url_a;   
  20.             }   
  21.         }   
  22.         $catid = $r['catid'];   
  23.         $time = $r['inputtime'];   
  24.         if(!$prefix) $prefix = $r['prefix'];   
  25.     }   
  26.     if(!isset($this->CATEGORY[$catid])) return false;   
  27.     $C = cache_read('category_'.$catid.'.php', '', 1);   
  28.     $tag = 0;   
  29.     if(preg_match('/:///',$C['url']))   
  30.     {   
  31.         $tag = 1;   
  32.         $arr_url = preg_split('///', $C['url']);   
  33.         $domain = 'http://'.$arr_url[2];   
  34.         $domain1 = 'http://'.$arr_url[2].'/';   
  35.         $info = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE `url` IN ('$domain', '$domain1') LIMIT 1");   
  36.         $crootdir = $info['parentdir'].$info['catdir'].'/';   
  37.     }   
  38.   
  39.     $categorydir = $C['parentdir'].$C['catdir'];   
  40.     $catdir = $C['catdir'];   
  41.   
  42.     //scofield 2010-3-16   
  43.     //for url roles add content title and category name   
  44.     $showtitle = strip_tags($r['title']);   
  45.     $catdirname = $C['catname'];   
  46.   
  47.     //scofield 2010-3-16   
  48.     //get root category name   
  49.     $catetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE  catid = $catid  LIMIT 1");   
  50.     $arrparentidarr = explode(',',$catetemp['arrparentid']);   
  51.     $rootidtemp = $arrparentidarr[count($arrparentidarr)-1];   
  52.     if($rootidtemp)   
  53.     {   
  54.         $rootcatetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE  catid = $rootidtemp LIMIT 1");   
  55.         $rootcatename = $rootcatetemp['catname'];   
  56.     }else  
  57.     {   
  58.         $rootcatename = $catdirname ;   
  59.     }   
  60.   
  61.     $fileext = $this->PHPCMS['fileext'];   
  62.     $year = date('Y', $time);   
  63.     $month = date('m', $time);   
  64.     $day = date('d', $time);   
  65.     $modelid = $C['modelid'];   
  66.     $urlruleid = $this->MODEL[$modelid]['show_urlruleid'];   
  67.     $urlrules = explode('|', $this->URLRULE[$urlruleid]);   
  68.     $urlrule = $page < 2 ? $urlrules[0] : $urlrules[1];   
  69.     if($this->MODEL[$modelid]['ishtml'])   
  70.     {   
  71.         if($prefix)   
  72.         {   
  73.             $contentid = $prefix;   
  74.         }   
  75.         elseif($PHPCMS['enable_urlencode'])   
  76.         {   
  77.             $contentid = hash_string($contentid);   
  78.         }   
  79.     }   
  80.     eval("$url = "$urlrule";");   
  81.     if($tag)   
  82.     {   
  83.         if(!(strpos($url, $crootdir)===0))   
  84.         {   
  85.             $url = $crootdir.$url;   
  86.         }   
  87.         $url_a[0] = $url;   
  88.         $url_a[1] = $domain1.str_replace($crootdir, '', $url);   
  89.     }   
  90.     else  
  91.     {   
  92.         $url_a[0] = $url_a[1] = $url;   
  93.     }   
  94.     return $url_a;   
  95. }  
function show($contentid, $page = 0, $catid = 0, $time = 0, $prefix = '')
{
    global $PHPCMS;
    if($catid == 0 || $time == 0 || $prefix == '')
    {
        $r = $this->db->get_one("SELECT * FROM `".DB_PRE."content` WHERE `contentid`='$contentid'");
        if($r['isupgrade'] && !empty($r['url']))
        {
            if($page>1)
            {
                $base_name = basename($r['url']);
                $fileext = fileext($base_name);
                $url_a[0] = $url_a[1] = preg_replace('/.'.$fileext.'$/','_'.$page.'.'.$fileext,$r['url']);
                return $url_a;
            }
            else
            {
                $url_a[0] = $url_a[1] = $r['url'];
                return $url_a;
            }
        }
        $catid = $r['catid'];
        $time = $r['inputtime'];
        if(!$prefix) $prefix = $r['prefix'];
    }
    if(!isset($this->CATEGORY[$catid])) return false;
    $C = cache_read('category_'.$catid.'.php', '', 1);
    $tag = 0;
    if(preg_match('/:///',$C['url']))
    {
        $tag = 1;
        $arr_url = preg_split('///', $C['url']);
        $domain = 'http://'.$arr_url[2];
        $domain1 = 'http://'.$arr_url[2].'/';
        $info = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE `url` IN ('$domain', '$domain1') LIMIT 1");
        $crootdir = $info['parentdir'].$info['catdir'].'/';
    }

    $categorydir = $C['parentdir'].$C['catdir'];
    $catdir = $C['catdir'];

    //scofield 2010-3-16
    //for url roles add content title and category name
    $showtitle = strip_tags($r['title']);
    $catdirname = $C['catname'];

    //scofield 2010-3-16
    //get root category name
    $catetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE  catid = $catid  LIMIT 1");
    $arrparentidarr = explode(',',$catetemp['arrparentid']);
    $rootidtemp = $arrparentidarr[count($arrparentidarr)-1];
    if($rootidtemp)
    {
        $rootcatetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE  catid = $rootidtemp LIMIT 1");
        $rootcatename = $rootcatetemp['catname'];
    }else
    {
        $rootcatename = $catdirname	;
    }

    $fileext = $this->PHPCMS['fileext'];
    $year = date('Y', $time);
    $month = date('m', $time);
    $day = date('d', $time);
    $modelid = $C['modelid'];
    $urlruleid = $this->MODEL[$modelid]['show_urlruleid'];
    $urlrules = explode('|', $this->URLRULE[$urlruleid]);
    $urlrule = $page < 2 ? $urlrules[0] : $urlrules[1];
    if($this->MODEL[$modelid]['ishtml'])
    {
        if($prefix)
        {
            $contentid = $prefix;
        }
        elseif($PHPCMS['enable_urlencode'])
        {
            $contentid = hash_string($contentid);
        }
    }
    eval("$url = "$urlrule";");
    if($tag)
    {
        if(!(strpos($url, $crootdir)===0))
        {
            $url = $crootdir.$url;
        }
        $url_a[0] = $url;
        $url_a[1] = $domain1.str_replace($crootdir, '', $url);
    }
    else
    {
        $url_a[0] = $url_a[1] = $url;
    }
    return $url_a;
}
 
 
 
说明下,这个函数是生成 文章内容页要用到的函数。
 
同样,在这个文件中还有 个 function category($catid, $page = 0, $isurls = 0, $type = 3) 这个函数
也需要修改。添加如下代码
 
 
view plaincopy to clipboardprint?
  1. //scofield 2010-3-16 add   
  2. $catdirname = $C['catname'];   
  3.   
  4. //scofield 2010-3-16   
  5. //get root category name   
  6. $parentidtemp = $C['parentid'];   
  7. $catetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE  catid = $parentidtemp  LIMIT 1");   
  8. $parentcatename = $catetemp['catname'].'/'.$catdirname;  
//scofield 2010-3-16 add
$catdirname = $C['catname'];

//scofield 2010-3-16
//get root category name
$parentidtemp = $C['parentid'];
$catetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE  catid = $parentidtemp  LIMIT 1");
$parentcatename = $catetemp['catname'].'/'.$catdirname;
 
 
完整的函数代码如下:
 
 
view plaincopy to clipboardprint?
  1. function category($catid, $page = 0, $isurls = 0, $type = 3)   
  2. {   
  3.     if(!isset($this->CATEGORY[$catid])) return false;   
  4.     $C = cache_read('category_'.$catid.'.php', '', 1);   
  5.     if($C['type'] == 0)   
  6.     {   
  7.         $modelid = $C['modelid'];   
  8.         $urlruleid = $this->MODEL[$modelid]['category_urlruleid'];   
  9.     }   
  10.     elseif($C['type'] == 1)   
  11.     {   
  12.         $urlruleid = $C['category_urlruleid'];   
  13.     }   
  14.     elseif($C['type'] == 2)   
  15.     {   
  16.         return $C['url'];   
  17.     }   
  18.     if(is_numeric($page)) $page = intval($page);   
  19.     $arrparentids = explode(',',$C['arrparentid']);   
  20.     $domain_dir = $domain_url = '';   
  21.     if(preg_match('/:///', $C['url']) && (substr_count($C['url'], '/')<4))   
  22.     {   
  23.         $url_a[0] = $C['parentdir'].$C['catdir'].'/index.'.$this->PHPCMS['fileext'];   
  24.         $url_a[1] = $C['url'];   
  25.         return $type<3 ? $url_a[$type] : $url_a;   
  26.     }   
  27.     else  
  28.     {   
  29.         $count = count($arrparentids)-1;   
  30.         for($i=$count; $i>=0; $i--)   
  31.         {   
  32.             if(preg_match('/:///', $this->CATEGORY[$arrparentids[$i]]['url']) && (substr_count($this->CATEGORY[$arrparentids[$i]]['url'], '/')<4))   
  33.             {   
  34.                 $domain_dir = $this->CATEGORY[$arrparentids[$i]]['parentdir'].$this->CATEGORY[$arrparentids[$i]]['catdir'].'/';   
  35.                 $domain_url = $this->CATEGORY[$arrparentids[$i]]['url'];   
  36.                 break;   
  37.             }   
  38.         }   
  39.     }   
  40.     $categorydir = $C['parentdir'].$C['catdir'];   
  41.     $catdir = $C['catdir'];   
  42.   
  43.     //scofield 2010-3-16 add   
  44.     $catdirname = $C['catname'];   
  45.   
  46.     //scofield 2010-3-16   
  47.     //get root category name   
  48.     $parentidtemp = $C['parentid'];   
  49.     $catetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE  catid = $parentidtemp  LIMIT 1");   
  50.     $parentcatename = $catetemp['catname'].'/'.$catdirname;   
  51.   
  52.     $fileext = $this->PHPCMS['fileext'];   
  53.     $urlrules = explode('|', $this->URLRULE[$urlruleid]);   
  54.     $urlrule = $page === 0 ? $urlrules[0] : $urlrules[1];   
  55.     eval("$url = "$urlrule";");   
  56.     if($C['type']==0 && $this->MODEL[$modelid]['ishtml'] && $domain_dir)   
  57.     {   
  58.         if(strpos($url, $domain_dir)===false)   
  59.         {   
  60.             $url_a[0] = $domain_dir.$url;   
  61.         }   
  62.         else  
  63.         {   
  64.             $url_a[0] = $url;   
  65.         }   
  66.         $url_a[1] = str_replace($domain_dir, $domain_url.'/', $url_a[0]);   
  67.     }   
  68.     else  
  69.     {   
  70.         $url_a[0] = $url_a[1] = $url;   
  71.     }   
  72.     return $type<3 ? $url_a[$type] : $url_a;   
  73. }  
function category($catid, $page = 0, $isurls = 0, $type = 3)
{
    if(!isset($this->CATEGORY[$catid])) return false;
    $C = cache_read('category_'.$catid.'.php', '', 1);
    if($C['type'] == 0)
    {
        $modelid = $C['modelid'];
        $urlruleid = $this->MODEL[$modelid]['category_urlruleid'];
    }
    elseif($C['type'] == 1)
    {
        $urlruleid = $C['category_urlruleid'];
    }
    elseif($C['type'] == 2)
    {
        return $C['url'];
    }
    if(is_numeric($page)) $page = intval($page);
    $arrparentids = explode(',',$C['arrparentid']);
    $domain_dir = $domain_url = '';
    if(preg_match('/:///', $C['url']) && (substr_count($C['url'], '/')<4))
    {
        $url_a[0] = $C['parentdir'].$C['catdir'].'/index.'.$this->PHPCMS['fileext'];
        $url_a[1] = $C['url'];
        return $type<3 ? $url_a[$type] : $url_a;
    }
    else
    {
        $count = count($arrparentids)-1;
        for($i=$count; $i>=0; $i--)
        {
            if(preg_match('/:///', $this->CATEGORY[$arrparentids[$i]]['url']) && (substr_count($this->CATEGORY[$arrparentids[$i]]['url'], '/')<4))
            {
                $domain_dir = $this->CATEGORY[$arrparentids[$i]]['parentdir'].$this->CATEGORY[$arrparentids[$i]]['catdir'].'/';
                $domain_url = $this->CATEGORY[$arrparentids[$i]]['url'];
                break;
            }
        }
    }
    $categorydir = $C['parentdir'].$C['catdir'];
    $catdir = $C['catdir'];

    //scofield 2010-3-16 add
    $catdirname = $C['catname'];

    //scofield 2010-3-16
    //get root category name
    $parentidtemp = $C['parentid'];
    $catetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE  catid = $parentidtemp  LIMIT 1");
    $parentcatename = $catetemp['catname'].'/'.$catdirname;

    $fileext = $this->PHPCMS['fileext'];
    $urlrules = explode('|', $this->URLRULE[$urlruleid]);
    $urlrule = $page === 0 ? $urlrules[0] : $urlrules[1];
    eval("$url = "$urlrule";");
    if($C['type']==0 && $this->MODEL[$modelid]['ishtml'] && $domain_dir)
    {
        if(strpos($url, $domain_dir)===false)
        {
            $url_a[0] = $domain_dir.$url;
        }
        else
        {
            $url_a[0] = $url;
        }
        $url_a[1] = str_replace($domain_dir, $domain_url.'/', $url_a[0]);
    }
    else
    {
        $url_a[0] = $url_a[1] = $url;
    }
    return $type<3 ? $url_a[$type] : $url_a;
}
 
 
 
注意,在 show方法中,多了 $showtitle , $catdirname ,$rootcatename 这三个变量。 第一个是内容的标题,第二个是当前分类的名称,第三个是某分类的父分类的名称。
这样就可以生成 类似 http://www.aa.com/分类1/标题_1.html 这样的路径。
当然,我们还需要在phpcms后台更改下 URL规则,以使得phpcms支持中文路径。这个下面我们再说。
我们再来看 category 方法。 也多了几个要用的 变量。 $catdirname , $parentcatename  第一个是 当前分类的名称,第二个是某分类的父分类的名称。
 
这几个变量,我们需要来更改phpcms的URL规则。
在phpcms后台->系统管理->其他设置->URL规则管理->管理URL规则。
我们找到 
Phpcms   category  √   {$categorydir}/index.{$fileext}|{$categorydir}/{$page}.{$fileext}   it/product/2.html    修 改 | 删除
这行,修改为
Phpcms  category   √   {$parentcatename}/index.{$fileext}|{$parentcatename}/{$page}.{$fileext} it/product/2.html    修 改 | 删除
 
 
再将
Phpcms   category   √  {$catdir}/index.{$fileext}|{$catdir}/{$page}.{$fileext} news/2_1.html    修 改 | 删除
修改为
Phpcms   category   √   {$parentcatename}/index.{$fileext}|{$parentcatename}/{$page}.{$fileext} news/2_1.html  修 改 | 删除
 
 
再将
Phpcms  show   √   {$year}/{$month}{$day}/{$contentid}.{$fileext}|{$year}/{$month}{$day}/{$contentid}_{$page}.{$fileext} 2006/1010/1_2.html  修 改 | 删除
修改为
Phpcms show   √  {$rootcatename}/{$showtitle}_{$contentid}.{$fileext}|{$rootcatename}/{$showtitle}_{$contentid}_{$page}.{$fileext} product/1_2.html 修 改 | 删除
 
修改后的 URL规则就可以支持中文路径了。
 
经过以上设置和修改,我们使得PHPCMS支持了中文路径,支持了中文URL。

热门栏目