使您的查询字符串看起来更好


Making your Query String look nicer

我想更改我的url:

items.php?category=all&page=2

为:

items/all/2

我知道我能够使用mod_rewrite做到这一点。我想知道的是使用mod_rewrite后,我能使用

<a href="items/all/2">

还是我还需要用

<a href="items.php?category=all&page=2">

我希望能够使用第一个。如果不能使用mod_rewrite,还有什么其他方法可以实现这个结果?

你可以使用mod_rewrite,之后你可以在你的代码中使用像这样的字符串<a href="items/all/2">,尝试使用下面的规则。

请注意,它是重写的,而不是重定向的,所以上面的链接将在apache中作为<a href="items.php?category=all&page=2">处理。

将以下规则放在根目录下的.htaccess文件中。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(['w]+)'(['w]+)'(['w]+)$ $1.php?category=$2&page=$3 [QSA,L]

http://httpd.apache.org/docs/current/mod/mod_rewrite.html rewriterule

为了获得更好看的url,您可以使用mod_rewrite。在.HTACCESS文件中使用这种规则:

RewriteRule ^voitures/([a-zA-Z0-9_-]+)$ /vehicules.php?gamme=$1 [L]

允许您通过example.com/voitures/xyz访问example.com/vehicules.php?gamme=xyz