A complicated mod_rewrite rule

Discussion in 'Apache' started by 70mas, Apr 22, 2008.

  1. #1
    Hello,

    I want to have some nice URLs like:
    http://example.com/shop/product/details/123

    and I have for example: /shop/product.php

    I'd like Apache to search for the right file from right to left. That means,

    it should at first look for
    /shop/product/details/123.php

    when it doesnt find, then
    /shop/product/details.php

    when it doesnt find, then finally
    /shop/product.php
    which is the right file.


    Note: If there is no such file at all (the match string is /), it should just pass it to /index.php

    Thank you in advance.

    70mas
     
    70mas, Apr 22, 2008 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Can that be done through mod_rewrite alone? If so, this might get you on the right track (not tested, barely checked):
    RewriteEngine On
    RewriteCond %{REQUEST_URI} (shop(/\w+)*)$
    RewriteCond %1.php -f
      RewriteRule .* /%1.php [L]
    RewriteCond %{REQUEST_URI} (shop(/\w+)*)/\w+$
    RewriteCond %1.php -f
      RewriteRule .* /%1.php [L]
    RewriteCond %{REQUEST_URI} (shop(/\w+)*)(/\w+){2}$
    RewriteCond %1.php -f
      RewriteRule .* /%1.php [L]
    RewriteRule shop/.* /shop/product.php [L]
    Code (markup):
    Seriously though, this should be handled by your application front controller or request handler, mod_rewrite is not a very elegant solution and has many limitations.
     
    krt, Apr 23, 2008 IP