通过mod_rewrite php将clean url转换为普通url

时间:2010-05-24 06:33:38

标签: php .htaccess

我在php中使用了干净的URL。现在我想将一个干净的URL转换为普通的php URL Like http://localhost/url/user/2/ahttp://localhost/url/user.php?id=2&sort=a

任何人都可以给我这样做吗?

我还有一个问题。有什么方法可以用.htaccess吗?

2 个答案:

答案 0 :(得分:1)

在根目录的.htaccess文件中:

RewriteEngine On
RewriteRule ^url/user/(\d+)/([a-zA-Z]?)$ /url/user.php?id=$1&sort=$2

应该这样做。

答案 1 :(得分:1)

我建议不要为每个模块编写特定的规则,但要建立一个前端控制器,它将接收所有请求并将它们发送到相应的模块。

RewriteEngine on
RewriteBase /
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule  ^(.*)$ index.php?uri=$1 [QSA,L]

因此,您最终会在脚本中使用$ _GET ['uri']参数,可以对其进行解析以获取所需的值