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

时间:2023-02-27
本文介绍了在 URL 中传递多个值时,PHP 中的 URL 重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究如何使用 Mod rewrite 修改 php 中的 URL.

I am currently researching how to modify URLs in php using Mod rewrite.

典型的 URL 可能如下所示:

A typical URL could look like this:

http://www.fitness.com/find_a_pt/?county=&constituency=211&gender=&action=search

现在在上面的例子中只选择了一个选区.未指定县或性别.

Now in the above example only a constituency has been selected. No county or gender has been specified.

现在上述选区指的是达特福德".

Now the above constituency refers to 'Dartford'.

所以如果 URL 是这样就好了:

So it would be good if the URL read:

http://www.fitness.com/find_a_pt/dartford

现在增加的复杂性是性别可能是:

Now the added complication is that a gender may be either:

1) 未选择 - 根据上面的网址

1) Not selected - as per the URL above

2) 男

http://www.ego3fitness.com/find_a_pt/?county=&constituency=211&gender=1&action=search

3) 女性

http://www.ego3fitness.com/find_a_pt/?county=&constituency=211&gender=&actiom=search

因此 URL 需要读取:

So the URLs would need to read:

1) http://www.fitness.com/find_a_pt/dartford

2) http://www.fitness.com/find_a_pt/dartford/male

3) http://www.fitness.com/find_a_pt/dartford/female

首先,URL 重写是否可以做到这一点,如果可以,有人可以提供一个示例供我使用.

Firstly is it possible to be this specific with the URL rewrites and if so could someone provide an example for me to work from.

提前感谢您的帮助.

推荐答案

我总是使用相同的 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.

很高兴有一个带有正则表达式规则的路由类来解析 uri.看一个例子 here 并阅读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)

这篇关于在 URL 中传递多个值时,PHP 中的 URL 重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:简单的 Mod 重写 下一篇:在 IIS7 上设置 Laravel

相关文章

最新文章