简体   繁体   English

通过mod_rewrite php将干净的url转换为普通的url

[英]convert clean url to normal url by mod_rewrite php

Hai

I have worked with clean URL in php. 我曾在PHP中使用干净的URL。 Now I want to convert a clean URL to normal php URL Like http://localhost/url/user/2/a to http://localhost/url/user.php?id=2&sort=a 现在我想将干净的URL转换为普通的php URL,例如将http:// localhost / url / user / 2 / a转换http://localhost/url/user.php?id = 2&sort = a

Can any one give me the way to do this? 有人可以给我办法吗?

also i have one more question. 我还有一个问题。 Is there is any way to do this with out .htaccess? 有没有办法用.htaccess做到这一点?

In your .htaccess file in your root directory: 在根目录的.htaccess文件中:

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

should do it. 应该这样做。

I'd suggest not to write specific rule for the every module, but make a front controller which will receive all requests and the dispatch them to the corresponding modules. 我建议不要为每个模块编写特定的规则,而要使前端控制器能够接收所有请求并将其分配到相应的模块。

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

So, you'll end up with $_GET['uri'] parameter in your script, which can be parsed to get required values 因此,您最终将在脚本中使用$ _GET ['uri']参数,可以对其进行解析以获取所需的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM