问题描述:
最近有SEO的兄弟求我帮忙将 discuz 论坛的帖子标题搞到 url 里面去,比如
帖子为 "为什么花这样红" ,对应的地址就是 weishenmohuazheyanghong-t-1-1.html
据说这样有助于搜索引擎搜录
问题解决:
我查看了discuz 的php代码,发现只需要做很小的改动就可以实现此功能:
现代码如下:
include/global.func.php 修改
function rewrite_thread($tid, $page = 0, $prevpage = 0, $extra = '') {
$pinyin = pinyin(getthread($tid));
return '<a href="'.$pinyin.'-t-'.$tid.'-'.($page ? $page : 1).'-'.($prevpage && !IS_ROBOT ? $prevpage : 1).'.html"'.stripslashes($extra).'>';
}
function rewrite_forum($fid, $page = 0, $extra = '') {
$pinyin = pinyin(getforum($fid));
return '<a href="'.$pinyin.'-f-'.$fid.'-'.($page ? $page : 1).'.html"'.stripslashes($extra).'>';
}
在文件最下添加
include_once 'phpsir_rewrite_pinyin.php';
新增 phpsir_rewrite_pinyin.php 文件内容
<?php
//phpsir
function pinyin($str,$ishead=0,$isclose=1)
{
global $pinyins;
global $charset;
$str = strip_tags($str);
if($charset == 'utf-8')
{
$str = iconv('utf-8','gbk',$str);
}
//echo $str."<br/>";
$restr = "";
$str = trim($str);
$slen = strlen($str);
if($slen<2) return $str;
if(count($pinyins)==0){
$fp = fopen(dirname(__FILE__)."/pinyin.db","r");
while(!feof($fp)){
$line = trim(fgets($fp));
$pinyins[$line[0].$line[1]] = substr($line,3,strlen($line)-3);
}
fclose($fp);
}
for($i=0;$i<$slen;$i++){
if(ord($str[$i])>0x80)
{
$c = $str[$i].$str[$i+1];
$i++;
if(isset($pinyins[$c])){
if($ishead==0) $restr .= $pinyins[$c];
else $restr .= $pinyins[$c][0];
}else $restr .= "-";
}else if( eregi("[a-z0-9]",$str[$i]) ){ $restr .= $str[$i]; }
else{ $restr .= "-"; }
}
if($isclose==0) unset($pinyins);
//echo $restr."<br/>";
$a = array("/\-{2,}/","/^\-{1,}/");
$b = array("-","");
$restr = preg_replace($a,$b,$restr);
return $restr;
}
function getthread($tid)
{
global $db;
global $tablepre;
$a = $db->query("select * from {$tablepre}threads where tid = $tid ");
$a = $db->fetch_array($a);
return $a['subject'];
}
function getforum($fid)
{
global $db;
global $tablepre;
$a = $db->query("select * from {$tablepre}forums where fid = $fid ");
$a = $db->fetch_array($a);
return $a['name'];
}
// end phpsir
?>
还有一点就是修改 .htaccess 加入
RewriteRule ^(.*)-f-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$2&page=$3
RewriteRule ^(.*)-t-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$2&extra=page\%3D$4&page=$3
搞定
llypp QQ 733905 于家中