phpcms中文URL路径解决方法

作者:袖梨 2022-06-25
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. $data = ob_get_contents();
  7. ob_clean();
  8. dir_create(dirname($file));
  9. $strlen = file_put_contents($file, $data);
  10. @chmod($file,0777);
  11. return $strlen;
  12. }
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_ur*l[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. $categorydir = $C['parentdir'].$C['catdir'];
  39. $catdir = $C['catdir'];
  40. //scofield 2010-3-16
  41. //for url roles add content title and category name
  42. $showtitle = strip_tags($r['title']);
  43. $catdirname = $C['catname'];
  44. //scofield 2010-3-16
  45. //get root category name
  46. $catetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE catid = $catid LIMIT 1");
  47. $arrparentidarr = explode(',',$catetemp['arrparentid']);
  48. $rootidtemp = $arrparentidarr[count($arrparentidarr)-1];
  49. if($rootidtemp)
  50. {
  51. $rootcatetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE catid = $rootidtemp LIMIT 1");
  52. $rootcatename = $rootcatetemp['catname'];
  53. }else
  54. {
  55. $rootcatename = $catdirname ;
  56. }
  57. $fileext = $this->PHPCMS['fileext'];
  58. $year = date('Y', $time);
  59. $month = date('m', $time);
  60. $day = date('d', $time);
  61. $modelid = $C['modelid'];
  62. $urlruleid = $this->MODEL[$modelid]['show_urlruleid'];
  63. $urlrules = explode('|', $this->URLRULE[$urlruleid]);
  64. $urlrule = $page
  65. if($this->MODEL[$modelid]['ishtml'])
  66. {
  67. if($prefix)
  68. {
  69. $contentid = $prefix;
  70. }
  71. elseif($PHPCMS['enable_urlencode'])
  72. {
  73. $contentid = hash_string($contentid);
  74. }
  75. }
  76. eval("$url = "$urlrule";");
  77. if($tag)
  78. {
  79. if(!(strpos($url, $crootdir)===0))
  80. {
  81. $url = $crootdir.$url;
  82. }
  83. $url_a[0] = $url;
  84. $url_a[1] = $domain1.str_replace($crootdir, '', $url);
  85. }
  86. else
  87. {
  88. $url_a[0] = $url_a[1] = $url;
  89. }
  90. return $url_a;
  91. }
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_ur*l[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 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. //scofield 2010-3-16
  4. //get root category name
  5. $parentidtemp = $C['parentid'];
  6. $catetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE catid = $parentidtemp LIMIT 1");
  7. $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'], '/')
  22. {
  23. $url_a[0] = $C['parentdir'].$C['catdir'].'/index.'.$this->PHPCMS['fileext'];
  24. $url_a[1] = $C['url'];
  25. return $type
  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'], '/')
  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. //scofield 2010-3-16 add
  43. $catdirname = $C['catname'];
  44. //scofield 2010-3-16
  45. //get root category name
  46. $parentidtemp = $C['parentid'];
  47. $catetemp = $this->db->get_one("SELECT * FROM `".DB_PRE."category` WHERE catid = $parentidtemp LIMIT 1");
  48. $parentcatename = $catetemp['catname'].'/'.$catdirname;
  49. $fileext = $this->PHPCMS['fileext'];
  50. $urlrules = explode('|', $this->URLRULE[$urlruleid]);
  51. $urlrule = $page === 0 ? $urlrules[0] : $urlrules[1];
  52. eval("$url = "$urlrule";");
  53. if($C['type']==0 && $this->MODEL[$modelid]['ishtml'] && $domain_dir)
  54. {
  55. if(strpos($url, $domain_dir)===false)
  56. {
  57. $url_a[0] = $domain_dir.$url;
  58. }
  59. else
  60. {
  61. $url_a[0] = $url;
  62. }
  63. $url_a[1] = str_replace($domain_dir, $domain_url.'/', $url_a[0]);
  64. }
  65. else
  66. {
  67. $url_a[0] = $url_a[1] = $url;
  68. }
  69. return $type
  70. }
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'], '/')PHPCMS['fileext'];
        $url_a[1] = $C['url'];
        return $type=0; $i--)
        {
            if(preg_match('/:///', $this->CATEGORY[$arrparentids[$i]]['url']) && (substr_count($this->CATEGORY[$arrparentids[$i]]['url'], '/')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
注意,在 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。

相关文章

精彩推荐