[英]Apache mod_rewrite path name as query parameters?
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: 我想使用Apache的mod_rewrite
,以便能够将路径的每个文件夹作为特定的查询参数,例如考虑以下内容:
Basic example 基本的例子
Url requested: http://domain.com/shoes/prada/image-1/
网址要求: http://domain.com/shoes/prada/image-1/
: http://domain.com/shoes/prada/image-1/
Page served: http://domain.com/?cid=shoes&bid=prada&pid=image-1
页面服务: http://domain.com/?cid=shoes&bid=prada&pid=image-1
: http://domain.com/?cid=shoes&bid=prada&pid=image-1
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
. 在这种情况下,有3个子文件夹请求( /shoes/
, /prada/
then image-1
),因此第一个子文件夹在实际页面中作为cid
传递,第二个作为bid
,第三个作为pid
。
Full example 完整的例子
However, I would also like it to serve a particular page depending on the number of sub-folders requested, eg 但是,我还希望它根据所请求的子文件夹的数量来服务特定页面,例如
Url requested: http://domain.com/shoes/prada/
网址要求: http://domain.com/shoes/prada/
: http://domain.com/shoes/prada/
Page served: http://domain.com/shop.php?cid=shoes&bid=prada
页面服务: http://domain.com/shop.php?cid=shoes&bid=prada
: http://domain.com/shop.php?cid=shoes&bid=prada
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). 到目前为止,我设法找到的是基于正则表达式的mod_rewrite
匹配,但我的路径会有很大差异,这就是为什么我希望根据访问的文件夹数量有条件(请注意,我不是很好)正则表达式 - 我认为一个通配符会对此有所帮助,但我不确定从哪里开始)。
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. 这与示例有点不同,但这是我首先想要的。
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: 这允许重写最多四个文件夹的URL,每个文件夹的“名称”作为参数给出,并且每个额外的深度级别将URL重写为单独的资源,例如:
http://x.com/shoes/prada/2011-high-heels/ -> http://x.com/product.php?tid=shoes&sid=prada&eid=2011-high-heels
Tested on http://martinmelin.se/rewrite-rule-tester/ 在http://martinmelin.se/rewrite-rule-tester/上测试过
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.