Practical eCommerce

CS-Cart

Manage Subscriptions · Subscribe Now · F.A.Q.'s

HOME · Monday, May 12, 2008

Training & Education

Video Tutorial: Eliminating Dynamic URLs with Mod_Rewrite

Improved search engine indexing and clean URLs in minutes.

By: Brian Getting
Comments: 14

Get started using Mod_Rewrite to clean up your website's dynamic URLs with this video tutorial presented by Practical eCommerce's online director Brian Getting. We'll look at using simple Mod_Rewrite commands in an .htaccess file to get rid of those ugly dynamic URLs that look like this:

http://www.sample-website.com/index.php?id=435&type=ugly%20urls&why=I%20do%20not%20know

By the end of this tutorial, you'll have a better understanding of what it takes to make an ugly, dynamic URL like the one above into a clean, search engine friendly URL like this:

http://www.sample-website.com/435/nice/clean/url

We start by creating a simple PHP script that will gather variables from the URL string and output them, which will allow us to verify that our Mod_Rewrite changes are working properly. From there we move on to re-mapping URLs using Mod-Rewrite directives in our .htaccess file.

This tutorial is intended for website developers that are hosting websites on servers running Apache web server with Mod_Rewrite enabled, and access to their website's .htacess file. Please contact your hosting company for more information about your account. A basic understanding of server-side scripting and website administration is assumed.

AdvertisementInfopia

Software Used: Adobe Dreamweaver 8
Required Server Software: Apache Web Server w/ Mod_Rewrite enabled
Sample Files: mod_rewrite_1.zip

Click the image below to launch the tutorial

Eliminating Dynamic URLs with Mod_Rewrite

This video tutorial requires Flash Player version 8 or above.

Blinklist | Del.icio.us | Furl | Ma.gnolia | Newsvine | Spurl | Reddit | Technorati

Published on Sunday, January 28, 2007

Comments:

Great

Posted by: Manisekaran
Thursday, January 25, 2007

I was wondering, is that Dreamweaver you're using or some other HTML editor? Nice tutorial... I think people are scared of mod_rewrite but it's really not difficult and can make a huge difference in usability and search engine optimization.

Posted by: Brandon Eley
Tuesday, February 06, 2007

In this particular tutorial, I am using Dreamweaver as a text editor. You could use any plain text editor, but I like to use some sort of IDE that has code highlighting and auto-indent. Another good choice (if you are on a mac) is an application called TextMate, which is also a great code editing tool.

Posted by: Brian Getting
Tuesday, February 06, 2007

Hello, perfect tutorial, but I have one question, if I try www.something.com/john to rewrite on www.something.com/index.php?user=john it doesn't work. I tried anything, for example RewriteRule ^(.*)$ index.php?user=$1 [nc] but it returns index.php, but i need to return john. I tryed the direct url RewriteRule ^http://www.something.com/(.*)$ index.php?user=$1 [nc] but it still doesnt work. The address with www.something.com/john/ working, because I put this (.*) / to rewrite script. Please somebody to help me with the line code.

Posted by: Filip Pavlis
Friday, February 09, 2007

Filip,

You need to put it out at least one directory, otherwise it runs through a loop where any address at your site (since they all pass the RewriteRule test) generates a call back to the index page, passing everything after your domain name along as a variable. That's why you get the output that you are getting.

Try doing something like this, which should work:

RewriteRule ^users/(.*) index.php?user=$1

Posted by: Brian Getting
Wednesday, March 07, 2007

I've been wondering about how to do this for years, but never tried. Didn't realize how easy it could be. Great benefit to my site, thanks.

- Josh

Posted by: Josh
Sunday, June 03, 2007

this did present one problem for me. I use local values for displaying links and images in my html code. Having my urls as such:

http://www.mysite.com/view/1234

is telling the site to look for images in view/1234/images instead of /images.

Is there a simple solution for this that wont require changed the src to each image and link on my site?

Posted by: josh
Sunday, June 03, 2007

How do you include chars like _ - etc. ?
I want someone mentioned in the thread before I want to create mysite.com/john..
however the only way i can use (.*) is if i have a directory /users/ .... but then people have to type /users/john to access johns profile... i want them to just have to write mysite/john
it works without the users directory if i use (a-z) etc... but then if the users name has an underscore or any other chars it won work...
how is this done so that you can also include _ - in the users name. thanks J

Posted by: john
Friday, August 24, 2007

Josh- You could try the BASE HREF meta tag in your pages, but I would recommend going with images links such as "/images/your_image.jpg" rather than "images/your_images.jpg".

This has many advantages, and is probably the best way to do things.

Posted by: Brian Getting
Friday, August 24, 2007

John- If your URL is something along the lines of:

/mysite/john_smith

You will want to have the value of "john_smith" passed as a variable ($1) to the script that is accessing that information. The script should then go through and automatically replace the "_" with a space (" ") using something like .gsub (for Ruby). Then the database is queried for the user's name. In most cases, as I imagine it, you will have a first name and last name column in the database, so you would actually want to split the name at the underscore, and query the database for the user with that first and last name combination.

Hope this helps some.

Posted by: Brian Getting
Friday, August 24, 2007

We have this code working fine on a solaris server, but when we moved our site to a Linux server the redirecting doesn't work. Any ideas? Thanks

Posted by: Julia
Wednesday, September 05, 2007

Brian's solution won't work as he suggested to change images src to 'images/mymage' to /images/myimage' .

For example, if my site is http://localhost/mysite and I change image src to "/images/myimage", then image src would be http://localhost/myimage instead of http://localhost/images/myimage. This also applies to style sheet,javascript.

Is there any other solution through .htacces?

Posted by: netmastan
Tuesday, October 02, 2007

Oh man...nice tutorial... and all that works, but how can i make to actually work with a real website?

do i have to replace index.php
for something else?

what i mean i'm only getting the:

out variables

#####

The following variables were passed in the URL string:

Variable 1: $1
Variable 2: $2
Variable 3: $3
Variable 4: $3

Your mod-rewrite script seems to be working fine.

####

what is the step to make work

i tried all this rules in my .htaccess

#RewriteRule ^ppc/(.*)/ index.php?var1=$1&var2=$2&var3=$3

#RewriteRule ^ppc/(.*)/ index.php?ppc=$1

RewriteRule ^abc/([^/\.]+)/?$ index.php?var1=$1&var2=$2&var3=$3 [L]

###

http://www.example.com/abc/index.php?var1=$1&var2=$2&var3=$3

it suppose to go to www.example.com/ppc/var/1/var2/var3 and so on

why is not working?


please some one help.

can some one help

thanks.

Posted by: juan
Tuesday, October 09, 2007

Wow, nice tutorial.

Thank you so much for this. It really helps, specially for SEO and nice pretty URLs. Thanks!

Posted by: Gab
Saturday, November 03, 2007

↑ Back to Top

Leave a comment:

Please enter the following security code exactly as it appears.


Comments are stripped of HTML code upon submission. All comments are submitted for approval prior to being published. Please allow up to 24 hours for the approval process to take place. Practical eCommerce reserves the right to remove any comment at any time for any reason.

 


Related Articles

Articles at Practical eCommerce related to Video Tutorial: Eliminating Dynamic URLs with Mod_Rewrite:

Related Podcasts

Podcasts at Practical eCommerce related to Video Tutorial: Eliminating Dynamic URLs with Mod_Rewrite:

RSS 2.0 Feeds

Atom 1.0 Feeds

Technorati Tags

Ecommerce Articles

Browse All Articles
Browse our complete archive of ecommerce articles.
Accounting, Management & Legal
Ecommerce articles related to managing a small business including ecommerce accounting, business strategy and legal considerations.
Conversion & Usability
Online business articles about converting web site visitors into customers and how to gauge and improve your business website's usability.
Development & Programming
Articles to help designers, developers and programmers create successful, search engine friendly ecommerce websites and improve existing ones.
Hosting, Infrastructure & Software
Articles for ecommerce businesses about ecommerce web hosting, business infrastructure, business strategy and helpful ecommerce & small business software.
Interviews & Profiles
Interviews with prominent ecommerce business personalities and profiles of successful online businesses.
Inventory & Shipping
Ecommerce articles about inventory management, ecommerce order fulfillment and product shipping considerations.
Marketing & Revenue Growth
Articles relating to online marketing, email marketing and using the Internet to growing your business.
Search Engine Optimization
Search engine optimization articles for ecommerce business owners, strategists, marketers and developers.
Shopping Carts & Online Payments
Articles covering ecommerce shopping cart platforms and options for choosing an online payment gateway.
Training & Education
Tutorials and articles providing training and education for ecommerce business owners and developers of ecommerce websites.

Search Articles

Ecommerce Community

Ecommerce Blogs
Read our blogs about ecommerce topics written by industry professionals.
Community Forum
Connect with other ecommerce professionals to trade advice and answers in our community forum.
Podcasts
Check out our ecommerce podcasts covering topics ranging from interviews to tutorials.
RSS Content Feeds
Subscribe to our RSS feeds and have fresh ecommerce content delivered to you.

Ecommerce Resources

Free Email Newsletter
Sign up for Ecommerce Notes, our free email newsletter for ecommerce business owners and developers.
Ecommerce Directory
Browse our directory of ecommerce products and services, or submit your own listing in our directory.
Ecommerce Glossary
Familiarize yourself with terminology or submit terms to help others with our Ecommerce Glossary.
Events Calendar
Find out about upcoming ecommerce events or invite other ecommerce professionals by posting your own event.
Press Releases
Browse ecommerce related press releases and post your own press release for distribution.
Ecommerce Store & Back Issues
Pick up back issues of Practical eCommerce magazine along with other merchandise from Practical Ecommerce

About Practical eCommerce

Frequently Asked Questions
Look at frequently asked questions regarded using our website, subscribing to our magazine and more.
Advertising Information
Information about advertising in Practical eCommerce magazine, on our website, or in our email newsletters.
Editorial Sharing
Learn about options for sharing our content with your visitors, customers or employees.
About Us
Learn more about Practical Ecommerce magazine and meet our staff.
Contact Us
Contact Practical Ecommerce at any time for more information. We'd love to hear from you.
AdvertisementEndiciaStone Edge TechnologiesBDXI

Copyright 2007 Confluence Distribution, Inc. and Practical eCommerce.
All Rights Reserved.

Privacy PolicyConditions of UseContact Us