且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在URL中传递多个值时,用PHP重写URL

更新时间:2023-02-24 10:01:07

我始终使用相同的mode_rewrite规则:

I always use the same mode_rewrite rules:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

使用此规则,所有内容都将存储到index.php. 因此,您可以***地使用PHP实现每个url逻辑.

With this rules everything is forwared to index.php. So you are free to implement every url logic with PHP.

您可以使用$_SERVER['REQUEST_URI']获取请求uri,然后做任何您想做的事情.

You can get the request uri with $_SERVER['REQUEST_URI'] and do whatever you want.

拥有一个带有正则表达式规则的Routing类来解析uri真是太好了. 在此处看看示例,并阅读 Zend,Code Igniter等大型框架是如何做到的. (顺便提一下,我提供的重写规则来自Zend Framework)

It is nice to have a Routing class with regex rules to parse the uri. Look at an example here and also read how the big frameworks like Zend, Code Igniter etc. do it. (The rewrite rule I provided is from Zend Framework by the way)