SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot Deep13's Avatar
    Join Date
    Aug 2004
    Location
    India
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need some help with Regex in mod_rewrite

    Hi,
    I am following the tutorial posted by dklynn but facing some issue...

    I am trying to generate mod_rewrite rule for following url sets:

    the url can be

    index.php?year=2003&sub=eco

    or

    index.php?range=2002-2005

    In short, in the url range variable is optional and pair of year and sub is optional.

    So according to the guide, i simply copied the example to mine:

    RewriteRule ^/?([0-9]+)(-([a-z0-9]+)(-([a-z]+))?)?\.html$ index.php?range=$1&year=$3&sub=$5 [L]
    This works pretty well if I have only range in the url or range, year and sub. But it does not work if I have only year and sub provided.

    So I tried using following:

    RewriteRule ^/?(([0-9]+)?)(-([a-z0-9]+)(-([a-z]+))?)?\.html$ index.php?range=$1&year=$3&sub=$5 [L]
    But it didn't work.

    Can anyone tell me where I am going wrong?

    Regards,
    Deep

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    Deep,

    What you didn't specify was the URLs that you were using to test (so I could see that the regex was correctly trying to match the {REQUEST_URI} string).

    Specifications:

    year: [0-9]{4}

    sub: [a-z]+

    range: [0-9]{4}-[0-9]{4}

    Quote Originally Posted by deep
    the url range variable is optional and pair of year and sub is optional.
    Your first code:
    Code:
    RewriteRule ^/?([0-9]+)(-([a-z0-9]+)(-([a-z]+))?)?\.html$ index.php?range=$1&year=$3&sub=$5 [L]
    That says that the first atom, with one or more digits, is NOT optional, the second atom is optional but starts with an hyphen followed by one or more lowercase letters or digits optionally followed by another hyphen and one or more lowercase digits. Clearly, this does not meet the requirement for EITHER (or both) range or year/sub combination.

    Your second code:
    Code:
    RewriteRule ^/?(([0-9]+)?)(-([a-z0-9]+)(-([a-z]+))?)?\.html$ index.php?range=$1&year=$3&sub=$5 [L]
    In this code, the second atom is optional but does NOT include an hyphen in the regex to match the - in the range definition and the third (optional) atom is merely a repeat of the above which does not meet the year/sub optional pair requirement (sub is optional within the pair).

    The use of the hyphens to both separate the year values in the range AND separate the atoms from each other COULD cause a problem.

    All that is what's wrong.

    From the specifications (above):

    Going back to the requirements as I specified above, we can generate the regex as an optional range and an optional year-sub pair, ((range)-)?(year-sub)?, which, when substituting directly, would look like:
    Code:
    RewriteEngine on
    RewriteRule (([0-9]{4}-[0-9]{4})-)?(([0-9]{4})-([a-z]+))?$ index.php?range=$2&year=$3&sub=$5 [L]
    It's all in being able to specify the contents of each variable and the logic for assembling them into the regex string.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •