在URL中传递多个值时,用PHP重写URL [英] URL rewriting in PHP when several values are being passed in the URL

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

问题描述

我目前正在研究如何使用Mod重写来修改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 =搜索

现在在上面的示例中,仅选择了一个选区.没有指定县或性别.

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.

拥有一个带有正则表达式规则的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)

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆