Apache mod_rewrite path name as query parameters?

后端 未结 1 1537
忘了有多久
忘了有多久 2021-01-18 16:49

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:

相关标签:
1条回答
  • 2021-01-18 17:06

    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:

    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/

    0 讨论(0)
提交回复
热议问题