Mod Rewrite help needed

Discussion in 'Apache' started by kavinga, Jun 24, 2007.

  1. #1
    Hi,
    I'm having some trouble with Mod Rewrite Rule in htaccess.

    here the code:

    RewriteRule ^directory/(.*)-(.*).php$ browse.php?cat=$2
    RewriteRule ^directory/(.*)-(.*)-(.*).php$ browse.php?cat=$2&pg_which=$3

    here the URL:
    http://www.websitehostsearch.com/directory/budget-web-hosting-1.php
    http://www.websitehostsearch.com/directory/budget-web-hosting-1-2.php

    first line working correctly but 2nd line was messed up.

    2nd line was used as for browse pages in directory. but when click on "next" page should be like "directory/budget-web-hosting-1-2.php". but the problem is "-2.php" showing as "category" not as the "next page".

    :confused:
     
    kavinga, Jun 24, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    RewriteRule ^directory/(.*)-(0-9]+)-([0-9]+).php$ browse.php?cat=$2&pg_which=$3 [L]
    RewriteRule ^directory/(.*)-(0-9]+).php$ browse.php?cat=$2 [L]

    Don't use the matchall/wildcard dot character unless you really need to. There must be loads of different ways of splitting up "budget-web-hosting-1-2" into 2 or 3 parts separated by the dash symbol. You're just assuming it'll know what you want in which backreference. The first rule only works because the quantifiers are greedy so the first * will take up as much of the string as possible. You've also made it a bit harder for yourself by using the same character as a separator (the dash) in the actual page titles itself.

    Consider:
    budget-web-hosting-1-2
    budget-web-hosting-1

    And apply your patterns:
    (.*)-(.*)
    (.*)-(.*)-(.*)

    Both strings match both patterns. If you had used a different separator other than the dash, you could have just swapped the order round - putting the most specific pattern first and the most general pattern last. Then if that extra page number is there, it'll get caught by the first pattern and you're sorted. However since you have at least two dashes anyway (because of the ones in the page title), everything will get caught by the first rule.. so you need to be more specific.
     
    rodney88, Jun 25, 2007 IP
  3. kavinga

    kavinga Well-Known Member

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #3
    thanks for the advice. I tried your way but it's not working that way. :(
     
    kavinga, Jun 25, 2007 IP