需要帮助重写规则,以使网址清洁和seo友好?

时间:2012-11-21 07:49:07

标签: regex .htaccess url-rewriting seo

当前模式为http://example.com/questions.php?qcid=25&name=java,我想将其设为http://example.com/25/java,其中 qcid 必须 [0-9] 名称必须可读和编码。如果它包含多于1个单词,则' - '连字符必须位于它们之间。例如面向对象

2 个答案:

答案 0 :(得分:1)

这将从数字开始,然后斜线,然后任何东西,并转换它们如上所示。但是,如果您需要某种url编码/解码,则必须在questions.php脚本中处理该位

RewriteRule ^(\d+)/(.+) /questions.php?qcid=$1&name=$2 [NC,L]

答案 1 :(得分:1)

在.htaccess

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([0-9]+)/([a-z0-9\-]+)(?!(.css)|(.js))$ questions.php?qcid=$1&name=$2 [NC]

在php中,您需要捕获名称并删除连字符并根据需要修复大写字母。举个例子,假设我们希望这个名字更低一些:

$_GET['name'] = "this-is-a-test-name";

$name = '';
$exploded = explode('-',$_GET['name']);
foreach($exploded as $piece) {
    $name .= ucfirst($piece);
}
// If you have PHP >= 5.3
// $name = lcfirst($name);
// Otherwise
$name{0} = strtolower($name{0});

echo $name;
相关问题