Sales and Marketing Technologies
Search
Do you want Breakthrough Results? Let us help you.
Click to boost online marketing results! Click to sign up for our FREE e-Newsletter
Custom Web Development E-commerce Solutions Web Consulting Web Maintenance Web Hosting CMS Solutions
Internet Marketing Consulting Local Search Marketing Pay Per Click Management Reputation Management SEO Services Social Networking Video SEO (VSEO)
Miami Children's Hospital Reflectx Services Dinnerware Depot U.S. Home Filter
What We Offer Altamonte Springs Office Orlando Office
Internet Marketing Blog Contact Us Refer a Client Employment
Blog

ModRewrite for Apache and IIS

Tuesday, September 19, 2006

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

Tags: , , , , , , , , , , ,

Terry 9/19/2006 4:23:00 PM

Add your comments:

Items in bold indicate required information.

Name :
 
Email :
 
Comments :
 
Latest Posts

Categories
Blogging (2)
Website Design (4)
Website Functionality (4)
Internet Marketing (85)
Company News (6)
Email Marketing (7)
General Marketing (11)
Google Android (2)
Local Search (10)
Mobile Search (1)
Offline Marketing (2)
PPC (12)
RSS (5)
Search Engine Optimization (30)
Social Media (5)
Social Networking (14)
General Programming (2)
SQL Programming (2)
Video Search Engine Optimization (3)

Archives

Blogroll
Yahoo!
Google
Technorati Profile

Feeds
Newsletter Signup
Enter your email address below and receive special offers.
From Our Customers
Sales and Marketing Technologies have two main strengths. They have been tremendous in helping us improve our organic and paid search programs, driving more and more traffic to our Web site every year. Their other strength has been in design and programming of the site to perform in a way that enables conversion. A bonus for us is also their strength in understanding our particular industry based on their experience.”

Phillip Barnes - Wedgewood Pharmacy, Direct Marketing Specialist
Our Work    |    Web Development    |    Internet Marketing    |    Case Studies    |    FAQ    |    About SMT    |    Press Room
Employment    |    Refer a Client    |    Contact Us    |    Internet Marketing Blog    |    Privacy Policy    |    Site Map

Sales & Marketing Technologies
Altamonte Springs: 220 E. Central Parkway, Suite 1010 • Altamonte Springs, FL 32701407-792-4848
Orlando: 301 E. Pine Street, Suite 150 • Orlando, FL 32801407-792-1951

© 2010, Sales & Marketing Technologies, Ltd. All rights reserved.
Website Design FL & Search Engine Optimization Services

Serving Altamonte Springs, Apopka, Casselberry, Celebration, Lake Mary, Longwood, Kissimmee, Maitland, Orlando, Ovideo, Windermere, Winter Park, Winter Springs and other areas throughout Central Florida and the United States.