Could someone please explain this mod rewrite to me?


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…

http://www.yoursite.com/product.php?productid=2&categoryid=3

into this…

http://www.yoursite.com/product/13/3/2-GB-MP3-player.html.
^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>”

Actually it’s quite the opposite. Maybe if you looked at it the other way around, you’d see how it works.

zz,

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

Regards,

DK

RewriteEngine on

RewriteRule ^article/([0-9]+)/([0-9]+)/[B](.*?)[/B].html$ /article.php?articleid=$1&amp;categoryid=$2

I have some doubt of above code at part B[/B]. cos * is quantifier or ? is also quantifier, for (.*) or (.?) is quit enough to someone need. what about B[/B]? I don’t understand this part.

Could anyone explain me about this part B[/B]?

Thank in advance

sovanndy,

RewriteEngine on

RewriteRule ^article/([0-9]+)/([0-9]+)/[B](.*?)[/B][SIZE="5"][COLOR="Blue"]\\[/COLOR][/SIZE].html$ /article.php?articleid=$1[SIZE="5"][COLOR="Red"]&amp;[/COLOR][/SIZE]categoryid=$2 [SIZE="5"][COLOR="Blue"][L][/COLOR][/SIZE]

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

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.
, B[/B] 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

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