Apache的mod_rewrite路径名查询参数? [英] Apache mod_rewrite path name as query parameters?

查看:153
本文介绍了Apache的mod_rewrite路径名查询参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Apache的的mod_rewrite ,以便能够采取作为一个特定查询参数的路径的每个文件夹,例如考虑以下几点:

I want to use Apache's mod_rewrite in order to be able to take each folder of a path as a particular query parameter, for example consider the following:

基本例如

URL中请求: http://domain.com/shoes/prada/image-1/

页服: http://domain.com/?cid=shoes&bid=prada&pid=image-1

在这种情况下,有3个要求(子文件夹 /鞋/ /普拉达/ 然后图像-1 ),所以第一个子文件夹通过在实际的页面担任 CID ,第二个为出价和第三为 PID

In this scenario, there are 3 sub-folders requested (/shoes/, /prada/ then image-1), so the first sub-folder is passed in the actual page served as cid, the second as bid and the third as pid.

完整的示例

不过,我也想它根据请求的子文件夹的数目,例如满足特定的页面。

However, I would also like it to serve a particular page depending on the number of sub-folders requested, e.g.

URL中请求: http://domain.com/shoes/prada/

页服: http://domain.com/shop.php?cid=shoes&bid=prada

到目前为止,所有我设法找到是的mod_rewrite 基于正则表达式匹配,但我的路径的也会相差很多,这就是为什么我想有根据条件访问的文件夹的数量(请注意,我不是说好与正则表达式 - 我想通配符将有助于解决这个问题,但我也不会知道从哪里开始)。

So far all I've managed to find is regex based matching for mod_rewrite but my path's will vary a lot, which is why I would like to have conditions based on the number of folders accessed (please note, I'm not that good with regex - I reckon a wildcard character would help with this, but I wouldn't be sure where to start).

任何帮助将大大AP preciated!这是pretty长篇大论,所以如果你需要任何更多的信息澄清,请让我知道!

Any help on this would be greatly appreciated! This is pretty long winded, so if you need any more info for clarifying, please let me know!

推荐答案

随着工作的一点点,我能够调整一些正则表达式,并得到了我想要的设置工作规则:

With a little bit of work I was able to tweak some regex and get a working rule set for what I wanted:

RewriteEngine on
RewriteRule ^(.*)/(.*)/(.*)/(.)?$ product.php?tid=$1&sid=$2&eid=$3 [L]
RewriteRule ^(.*)/(.*)/(.)?$ brand.php?tid=$1&sid=$2 [L]
RewriteRule ^(.*)/(.)?$ shop.php?tid=$1 [L]

这是对的例子有点不同,但它是我打算在首位。

This is a bit different to the example, however it's what I intended for in the first place.

这允许对URL的多达四个文件夹深重写,以每个文件夹的名被作为参数,和深度每增加一个级别重写URL到例如单独的资源:

This allows for the rewriting of url's up to four folders deep, with the "name" of each folder being given as a parameter, and each additional level of depth rewriting the url to a separate resource for example:

http://x.com/shoes/prada/2011-high-heels/ -> http://x.com/product.php?tid=shoes&sid=prada&eid=2011-high-heels

测试在 http://martinmelin.se/rewrite-rule-tester/

这篇关于Apache的mod_rewrite路径名查询参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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