Apache mod_rewrite path name as query parameters?

I want to use Apache mod_rewriteto be able to take each folder of the path as a specific request parameter, for example, consider the following:

Basic example

Url required: http://domain.com/shoes/prada/image-1/

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

In this case the requested sub-folder 3 ( /shoes/, /prada/then image-1), so the first subfolder is transmitted to the actual page served as a cidsecond like bid, and the third - both pid.

Full example

However, I would also like it to serve a specific page depending on the number of subfolders requested, for example.

Url required: http://domain.com/shoes/prada/

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

So far, all I have managed to find is regular expression matching for mod_rewrite, but my path will be very different, so I would like to have conditions based on the number of folders available (note, m is not so good with regular expression - I I believe that a wildcard would help with this, but I would not be sure where to start).

Any help on this would be greatly appreciated! This is a pretty long wind, so if you need more information to clarify, let me know!

+5
source share
1 answer

With a bit of work, I was able to set up some regex and get the 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]

, , .

URL- , "" , URL- , :

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/

+7

All Articles