为什么我在url重写中有一个空值

时间:2014-06-12 08:34:38

标签: php .htaccess

我创建了一个自动更改我的网址的功能,但是当我点击链接时,网址已更改,但我没有任何结果。

这是我的功能:

$rewrite = 'yes';
function get_info_link($id){
    global $rewrite;
    if ($rewrite=='yes') {
        $link = 'person/'.$id.'.html';
    } else {
        $link = 'information.php?id='.$id; 
    }
    return $link;
}

这是我调用函数时的链接

<th>
    <a href="<?php echo get_info_link($resultat['id'])?>">
        Voir tous les infos
    </a>
</th>

在。 htaccess文件我使用了这个规则

RewriteEngine on
RewriteRule ^(person)/([0-9]+)\.html$ information.php?id=$1 [L]

2 个答案:

答案 0 :(得分:0)

在你的重写规则中,你获取第一个捕获的表达式,你正在取“人”字符串,而不是之后的数字。

试试这个:

RewriteEngine on
RewriteRule ^(person)/([0-9]+)\.html$ information.php?id=$2 [L]

或者这个:

RewriteEngine on
RewriteRule ^person/([0-9]+)\.html$ information.php?id=$1 [L]

两者都这样做,但我不知道你是否想要捕捉“人”字符串。

答案 1 :(得分:0)

你的问题是规则:

RewriteRule ^(person)/([0-9]+)\.html$ information.php?id=$1

你开始计数1而不是零。所以这是对的:

RewriteRule ^(person)/([0-9]+)\.html$ information.php?id=$2

另外,你可以做得更好:

RewriteRule ^person/([0-9]+)\.html$ information.php?id=$1

基本上,您可以获得每个(...)代币。