Search

Understanding Mod_Rewrite

In order to really understand mod rewrite, you need to be comfortable using regular expressions.

If you are completely in the dark about regular expressions, I suggest reading up on the subject before you attempt to learn how to use mod_rewrite, but I will give a short intro to Regular Expressions here.

A Regular Expressions is a sort of extremely flexible wildcard search that has it’s own set of symbols for matching pretty much any string.

A basic wildcard search would be something like this: *.html which would find any file that ends with .html.

Regular expressions do the same sort of thing, but as I said, are much more flexible. The same search with regex would look like this: .*\.html$

In the regex above, the . (dot) matches any single character, the * (asterisk) matches 0 or more of the preceding character, since we are looking for a literal dot, the . before html must be escaped, and finally the $ (dollar sign) means the string should end there.

More Regex Rules
abc would match abc
a sequence of caracters enclosed by brackets matches only one character:
[abc] would match: ‘a’, ‘b’, or ‘c’

[a-z] matches any lowercase character from a – z

[0-9] matches any number
the ‘*’ asteris after any character will match 0 or more occurences of the preceding character, while the ‘+’ will match 1 or more.

so [a-z]* will match 0 or more lowercase letters in a row. like :’aajfalsdjflasdjfalsdjfalsdfj’

[a-zA-Z0-9_-]+ would match 1 or more of the characters shown within the brackets.

The dot (.) is kind of like a wildcard and matches a single character, except line break characters

Mod rewrites also rely on something called grouping in regular expressions.

() parenthesis are used to define a group. Anything you put in a group goes into a variable. So if you had somthing like:

Ill use your example to show you Grouping.

You want to change this dynamic url
/prod.asp?pid=6435&color=green&size=medium
to this
/products/6435/green/medium

This is what you would use:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule products/([0-9]+)/([a-z]+)/([a-z]+)$ /prod.asp?pid=$1&color=$2&size=$3

As you can see here there are 3 groups.
1. ([0-9]+) , ([a-z]+) , and ([a-z]+)
these 3 are put into the variables: $1, $2, and $3

So to summarize, what you are doing with a mod rewrite is asking your webserver to check each url when it comes in, to see if it matches the pattern products/([0-9]+)/([a-z]+)/([a-z]+)$, then its going to get sent to /prod.asp?pid=$1&color=$2&size=$3 with the variables from the groups.

I hope this helps.

Resources
A great site to learn Regular Expressions is Regular-Expressions.info

Here is a good tool that may help you figure it out(http://www.webmaster-toolkit.com/mod_rewrite-rewriterule-generator.shtml) , but you really should read the Regular expressions tutorial first.

If you enjoyed this post, make sure you subscribe to my RSS feed!


Leave a Comment