最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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?
- 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;
- }
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?
- //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'];
//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?
- 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'] && !emptyempty($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 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?
- //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;
//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?
- 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;
- }
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。
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22