SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict blizzman24's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Could someone please explain this mod rewrite to me?

    Code:
    RewriteEngine on
    
    RewriteRule ^article/([0-9]+)/([0-9]+)/(.*?).html$ /article.php?articleid=$1&categoryid=$2
    I've been trying to find a site that explains what each part of this code does but I can't seem to find it. I understand some of the regular expression, but can't seem to grasp what "$1" or "$2" are for. what do those do? What does the "^" and "$" after the .html serve?

    all I know is that it turns this...
    HTML Code:
    http://www.yoursite.com/product.php?productid=2&categoryid=3
    into this...

    Code:
    http://www.yoursite.com/product/13/3/2-GB-MP3-player.html.

  2. #2
    SitePoint Enthusiast Syam's Avatar
    Join Date
    Mar 2006
    Location
    Kochi, India
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    ^article/([0-9]+)/([0-9]+)/(.*?).html$
    It means "beginning with 'article/<some number>/<again some number>/<anything>' and ends with '.html' ".

    ^ means starting position of the string.
    $ means ending position of the string.

    $1 is the "<some number>" part.
    $2 is the "<again some number>" part.
    $3 (not used in this regex) will be "<anything>"

    http://en.wikipedia.org/wiki/Regular_expression#Syntax

  3. #3
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blizzman24 View Post
    all I know is that it turns this...
    HTML Code:
    http://www.yoursite.com/product.php?productid=2&categoryid=3
    into this...

    Code:
    http://www.yoursite.com/product/13/3/2-GB-MP3-player.html.
    Actually it's quite the opposite. Maybe if you looked at it the other way around, you'd see how it works.
    Saul

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

    Have a read of the mod_rewrite tutorial linked in my signature.

    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

  5. #5
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Code:
    RewriteEngine on
    
    RewriteRule ^article/([0-9]+)/([0-9]+)/(.*?).html$ /article.php?articleid=$1&amp;categoryid=$2
    I have some doubt of above code at part (.*?). cos * is quantifier or ? is also quantifier, for (.*) or (.?) is quit enough to someone need. what about (.*?)? I don't understand this part.


    Could anyone explain me about this part (.*?)?

    Thank in advance
    Last edited by sovanndy; Apr 6, 2010 at 08:34. Reason: miswritting

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,605
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    sovanndy,
    Code:
    RewriteEngine on
    
    RewriteRule ^article/([0-9]+)/([0-9]+)/(.*?)\.html$ /article.php?articleid=$1&amp;categoryid=$2 [L]
    The above code in red MUST be &, not the HTML version of the & character! The blue code (Last flag) is necessary to terminate your mod_rewrite block statement the same way that ; is commonly used in programming languages. Also, escape the dot character in the regex.

    (.*?) is the non-greedy version of (.*) but the ? is really irrelevant as you've specified EVERYTHING up to but NOT including .html at the end of the {REQUEST_URI} string. In this case, the ? is the modifier of the * metacharacter, otherwise, it would be the metacharacter for zero or one of the preceding character.

    Regards,

    DK
    Last edited by dklynn; Apr 6, 2010 at 16:33. Reason: Last flag
    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

  7. #7
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    According with your saying
    In this case, the ? is the modifier of the * metacharacter, otherwise, it would be the metacharacter for zero or one of the preceding character.
    , (.?) is quit enouch to what...., but not (.*?)


    cos ? have to modify to preceeding metacharacter like . , but not +, *

    . is identified for any characters, but * is not identified for any characters, but just modifier for any characters......

    Could you explain me clearly?


    Thank

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

    Metacharacters:

    . is ANY character

    ? is zero or one of the previous character

    + is one or more of the previous character

    * is zero or more of the previous character

    using +? or *? is the "non-greedy" form of the "greedy" metacharacter

    What does that mean for regex within mod_rewrite? NOTHING! I can't see any use for the ? as a metacharacter modifier in mod_rewrite. It IS, however, very useful in parsing text strings in scripts. Have a read of Friedl's Mastering Regular Expressions, it's the definitive treatment of Regex.

    Regards,

    DK

    Quote Originally Posted by sovanndy View Post
    According with your saying , (.?) is quit enouch to what...., but not (.*?)


    cos ? have to modify to preceeding metacharacter like . , but not +, *

    . is identified for any characters, but * is not identified for any characters, but just modifier for any characters......

    Could you explain me clearly?


    Thank
    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
  •