使用php清理网址对seo友好

时间:2011-09-24 13:11:36

标签: php url seo

我正在将我的网址变得友好。

我的博客网址目前如下:

http://domain.com/news/view-article.php?id=23+category=qrops+title=moving-your-pension-abroad---what-are-the-benefits?

我如何确保像@这样的人物? ><不要出现在我的网址中?

我怎样才能避免重复---?

生成网址的代码如下:

<a class="small magenta awesome" title="View full article" href="view-article.php?id='.$row['id'].'+category='.strtolower($row['category']).'+title='.strtolower(str_replace(" ","-",$row['title'])).'">View full article &raquo;</a>

很确定我做错了什么,但我正在努力......

帮助表示赞赏..

我将继续在apache中使用mod_rewrite

2 个答案:

答案 0 :(得分:4)

我曾经使用过这个功能

function SEO($input){ 
    //SEO - friendly URL String Converter    
    //ex) this is an example -> this-is-an-example
    $input = str_replace("&nbsp;", " ", $input);
    $input = str_replace(array("'", "-"), "", $input); //remove single quote and dash
    $input = mb_convert_case($input, MB_CASE_LOWER, "UTF-8"); //convert to lowercase
    $input = preg_replace("#[^a-zA-Z]+#", "-", $input); //replace everything non an with dashes
    $input = preg_replace("#(-){2,}#", "$1", $input); //replace multiple dashes with one
    $input = trim($input, "-"); //trim dashes from beginning and end of string if any
    return $input; 
}

例如,您可以通过

使用它
echo "<title>".SEO($title)."</title>";

答案 1 :(得分:1)

我使用这个甜蜜的功能来生成SEO友好的URL

function url($url) {
        $url = preg_replace('~[^\\pL0-9_]+~u', '-', $url);
        $url = trim($url, "-");
        $url = iconv("utf-8", "us-ascii//TRANSLIT", $url);
        $url = strtolower($url);
        $url = preg_replace('~[^-a-z0-9_]+~', '', $url);
        return $url;
}