ModRewrite RegEx problem

Discussion in 'PHP' started by sonrahton, Aug 31, 2009.

  1. #1
    Hi dear mates

    The following code is a snapet from my .htaccess file:
    The problem is, if the generated html address has to much "_" signs (or combinations of "_TEXT_"), the CatID will interpretaded as 10 instead of 102 (as example). I tested some addresses for this cognition.

    However, I'm sure there is a way to improve my RewriteRule. I'm not a RegEx Expert and my time is running ^^ so I decided to create this post.

    Best regards,
    Sonrahton
     
    sonrahton, Aug 31, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try replacing the first .* by .*?
     
    premiumscripts, Aug 31, 2009 IP
  3. Sudoku-Master

    Sudoku-Master Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if your catid contains no _ you can try this:
    RewriteRule ^([^_]+)_(.+).html$ ./index.php?CatID=$1&urltitle=$2 [L] 
    Code (markup):
    if your catid contains only numbers you can try this:
    RewriteRule ^([0-9]+)_(.+).html$ ./index.php?CatID=$1&urltitle=$2 [L] 
    Code (markup):
     
    Sudoku-Master, Aug 31, 2009 IP
  4. sonrahton

    sonrahton Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yess, it worked.

    I used the solution from premiumscripts but good to see another solution(s).

    Thanks very much,

    Sonrahton
     
    sonrahton, Aug 31, 2009 IP
  5. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Just to explain, the "?" makes the match ungreedy. It will try to match as little as possible.
     
    premiumscripts, Aug 31, 2009 IP
  6. sonrahton

    sonrahton Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    After I worked more on my project, I needed to use the solution from Sudoku-Master, becuase:

    - ModRewrite shall just be active..if there is an ID in the URL. Otherwise you could have problems if you use templates with TinyMCE.


    Greetings, Sonrahton
     
    sonrahton, Sep 16, 2009 IP