Mod Rewrite Problem (Invalid Expr Porb)

Discussion in 'PHP' started by mark84, Jul 1, 2007.

  1. #1
    I wanted to do this in a directory,

    infinite-something-news-23.html

    to

    /mysite/php/news_details.php?news_id=$2

    and i have this placed in the .htaccess

    RewriteRule ^([a-z]|[0-9]|[-.]|[^/$&?=\s])*\-news-([0-9]+)\.(html|shtml|htm)$ /myportal/php/news_details.php?news_id=$2 [NC,NS,PT,L]

    Now the mapping works fine, BUT when i try something little invalid like
    infinite-something-news-2alpha3.html then the browser just hangs (FF, IE both) for infinite time and Apache load raises to 100%.

    I m not sure abt the flags either, tried with or without them. Didnt work.

    Can someone please help me with fixing the rule ?
    Appereciate any help. Thank you.:)
     
    mark84, Jul 1, 2007 IP
  2. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #2
    try this. should work pretty well.
    
    RewriteEngine on
    RewriteRule ^([^\s=&$\?][a-z0-9-]+(?:news-([0-9]{1,}))(?:\.s?html?))$ /myportal/php/news_details.php?news_id=$2 [L]
    
    PHP:
     
    ansi, Jul 2, 2007 IP
  3. mark84

    mark84 Peon

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Works file, thanks a lot. :)
    Just added NC flag to make it case insensitive.

    btw, what does : (colon) means in regular expression.
    I mean what is its effect in this case ?
     
    mark84, Jul 2, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    The ?: means the group (expression between parenthesis) where it's in won't be captured in a variable, like $1.
     
    nico_swd, Jul 2, 2007 IP
  5. mark84

    mark84 Peon

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Got it. Thanks. :)
     
    mark84, Jul 2, 2007 IP
  6. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #6
    no point it capturing what you don't need :)
     
    ansi, Jul 2, 2007 IP
  7. mark84

    mark84 Peon

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yes, ofcourse. Gotta love efficiency. :)
     
    mark84, Jul 2, 2007 IP