September 19, 2006

ModRewrite for Apache and IIS

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 Apache 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
Options +FollowSymLinks

# Duh
RewriteEngine on

# Everything that follows is in relation to the root
RewriteBase /

# 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:

[ISAPI_Rewrite]

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:

14 comments:

SJP Blogger said...

I seen that your blog is is about Website Promotion so I wanted to share a resource that does exactly that. It promotes websites through Website Directory Submission and Email Marketing. Hope it may be helpful..

Terry Howard said...

I'm going to actually leave this piece of spam here as an example of how much an idiot this guy is. Uh, perhaps when spamming for backlinks on blogs, you might actually make one of the 2 link attempts actually resolve. Moron.

Anonymous said...

hmmm...

I'll just mention IIS Mod-Rewrite which is a mod_rewrite compatible module for IIS and supports .htaccess too.

Terry Howard said...

Hey thanks, good to know there an alternative to ISAPI out there.

TerryB said...

I've been trying to find information on this that I can understand for a long time! I was starting to think that I was stupid. I got some advice from my web hosting company but that went straight over my head!

Well done for communicating on my level!

Anonymous said...

So do you think that using Apache along with something like Rasof will be truly beneficial?

Anonymous said...

Thanks for your valuable information.

It was really of use to me.

-Prabakaran.K

www.usjobcareer.com

The No.1 Job and Career Search Portal

Webplore said...

Good tips ,its very much useful and helpful tips....
Webplore

Sean said...

I am very happy that I found this posting! I enjoyed it very much! Thank you for sharing with us!

1984 Ford Galaxie AC Compressor said...

I have gone through your post and found some information which is quite important for me.
So, please keep it up with your kind of posts.

Anonymous said...

This is a better-quality article as they all are. I make fun of been wonder wide this an eye to some beat now. Its great to receive this info. You are fair and balanced.

Isuzu FTR Truck Turbo

LBM Direct Marketing said...

Hi,
This is very informative topic for me and I like it this post.good job and keep it up

TheBusinessMarketingAgency said...

Thanks for giving such a great article!

Marketing Company

Unknown said...

Such clever work and reporting! Keep up the great works guys I've added you guys to my blogroll.seo company leeds