1

我在使用 .htaccess 重写 URL 时遇到问题,当 URL 看起来像原始 url 时:http ://www.example.com/page.php?id=1&name=test 重写后:http:// /www.example.com/page-1-test它工作正常

但经理要求用连字符而不是空格,所以我这样做了

$name = str_replace(" ", "-", $name);

然后我回显 $page_name http://www.example.com/page.php?id=2&name=test-some-other-text

所以它看起来像这样http://www.example.com/page-2-test-some-other-text

当我尝试 $_GET['id']; 时,它需要 2-test-some-other-text 作为 id

这是我在 .htaccess 中的重写规则

RewriteRule page-(.*)-(.*)$ page.php?id=$1&name=$2
4

1 回答 1

3

由于 ID 是数字,请尝试以下规则:

RewriteRule page-([0-9]+)-(.*)$ page.php?id=$1&name=$2

它只会匹配 ID 的数字,然后在最后的破折号处结束,而不是继续尝试贪婪匹配。

于 2012-05-27T18:12:08.217 回答