Understanding Mod_Rewrite
- Posted by Badi Jones on November 18th, 2005 - Comment on this Post »
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!
- Link Requests Don’t Work. Unless …
- Useful SEO Bookmarklets
- I Guess Anyone Can Be an SEO Expert These Days
- SEO Commandments
- .EDU: We Trust You!
- Domain Age, Google Trust and iPods
- 34 Industry Leaders Rate Search Ranking Factors
- Finally! Google Shows us the Backlinks
- Google Moves Search Results Completely Below Fold
- Analyzing Outbound Links: Who Are You Linking To?
- SEO Glossary: SEO from A to Z
- Search Operator Guide for Google, MSN, and Yahoo
- The Essential Guide to Link Building
- Precision SEO
- 14thc.com SEO Interview Series
- Indexed Pages Tool Updated
- SEO Tools From MSN
- Links for Today
- One Benefit of Keywords in URLs for SEO
- My Experience With PRWeb
- Matt Cutts Interview Recapped
- Top Ten SEO Mistakes and How to Avoid Them
- The Infamous Google Sandbox
- Audio Interview with Matt Cutts
- On-Page Search Engine Optimization Techniques
- Understanding Mod_Rewrite
- Using Google Alerts to Track Progress
- Google Washing, Dark Forces, and the Sandbox
- Matt Cutts Interview
- Worst Link Exchange Ever
- Search Engine Ranking Factors
- Rel NoFollow up
- Detect nofollow Links in FireFox
- Google Sitemaps for Blogger Blogs
- Does Yahoo Penalize Affiliate Sites?

Leave a Comment