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

URL rewriting in PHP when several values are being passed in the URL(在 URL 中传递多个值时,PHP 中的 URL 重写)
本文介绍了在 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 重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)