September 19, 2006 ByTerry
There’s a lot of ModRewrite tutorials out there, but I always had a hard time finding a pretty clear cut explanation for the most common uses: redirecting and making search engine friendly URLs out of query string dynamic ones. Finding a simple laid out example was always a bear as well. A friend recently asked me for a rundown on using ModRewrite for both Apach and IIS servers and I wrote out a pretty good email explanation and thought, hey, perfect blog post. So here goes:
“Rewrite” is a misleading term, nothing it really getting rewritten so to speak. What is happening is you are putting a matching statement first and then what content to serve. This statement sorta says, “If someone asks for this URL, then show them the content that would get served with this address”. When you throw a 301 modifier at the end you say, “If someone asks for this URL, give them this content by redirecting them to it.”
For Apache you would put something like the following in your .htaccess file. You can proceed a line with # to make it a comment or note that doesn’t get processed:
# Something for Apache that is required for ModRewrite module to work
# Everything that follows is in relation to the root
# Redirecting via a 301 to a new page, ^ starts the match phrase to look for, $ ends it
RewriteRule ^RequestedPage\.html$ /GetBouncedHere.html [NC,R=301,L]
# This is the traditional ModRewrite for search engine friendly URLs
RewriteRule ^([a-zA-Z0-9]+)/pictures/([0-9]+)\.html$ /yourcode.php?type=pictures&forwho=$1&recID=$2 [NC,L]
NC means “no case” specific matching, R=301 is the type of redirect to use, L means this is the last rule, after running this stop and server the page. Inside parenthesis go your variables, things that can change. You have to declare what type of characters these can be, in this case, alpha numerical of either case. The + at the end means an infinite number of character would be allowed, without it it means one character of those types. For each set in parenthesis you plug a $1, $2, $3, etc… into where that variable needs to go into your dynamic URL. They don’t have to be in order, you could call $2 before $1. The above would allow you to do something like /joemama/pictures/28.html and serve up a dynamic page that pulls in the record ID 28 from the pictures database for joemama, etc… Because we left out the R=301 we get a “Rewritten” URL instead of being permanently redirected.
For IIS it’s the same concept just slightly different syntax and it goes in the httpd.ini file:
RewriteRule /OldPage\.html /NewPage\.html [I,RP,L]
RewriteRule /doc(.*)\.html /yourcode.asp\?document=$1 [I,L]
Just about the same except your have to escape (use \ ) on both ends, don’t need the ^ and $ to mark the beginning and end of phrase matches. The modifiers at the end differ slightly, L is the same, I means Ignore case, and RP is redirect permanent. Everything else is nearly identical.
Note: Technically in Apache you can use [a-zA-Z0-9] and (.*) interchangeable, but by limiting the type of characters people can try and stick in the URL which gets passed into your query for your database, you prevent errors and security breaches. That’s why Apache has that method available and is preferred, at least I think so.
The best tutorials and info on these I have found are: