Cleaning Up Your URL Query String using Rewrite Engine

By John Kim | September 6th, 2009      

Search Engine Optimization is a huge industry where companies get paid millions of dollars to boost their rankings to the top of Google. Now this article will not try to explain all the little tricks to achieve a higher search rank, but one way to improve the likelihood of having better SEO would be to clean up your damn URL.

If you are hosted onto an apache server then please continue, otherwise this information would be complete and utter garbage to you.

So how can you clean up a url that looks like this:

/certificate.php?folder=men&sort=date&order=desc&page=1

To a url that looks like this:

/certificate/men/date/desc/1/




.htaccess’s Rewrite Engine is the key

Now to keep things short and sweet, a .htaccess file allows a developer to control server functionality, and one of these functionality would happen to be the Rewrite Engine. The Rewrite Engine allows you to change your fugly url’s into a very sexy looking url that is easy on the eyes.

What you would first have to do is open up notepad or any text editing tool and write this code into it

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^certificate/(\w+)/(\w+)/(\w+)/(\w+)/$ /certificate.php?folder=$1&sort=$2&order=$3&page=$4 [nc]

Before saving you should understand how this file works.

These two lines of code is basically saying to turn on the rewrite engine, and should be included anytime you want to use rewrite engine.

Options +FollowSymlinks
RewriteEngine on




dissecting the RewriteRule

There are four parts to the last line.

RewriteRule ^certificate/(\w+)/(\w+)/(\w+)/(\w+)/$ /certificate.php?folder=$1&sort=$2&order=$3&page=$4 [nc]

The red code is just the name of the function you are using called the RewriteRule, the blue code is the url pattern you want the server to watch out for, the green code is the real url you want the server to reference to, and the orange code is for options you want to set. In this case the option means no-case meaning the RewriteRule is case insensitive.

Now because I don’t want this to be a lesson about Regular Expression I’ve created a simple diagram to explain what is going on.

So as you can see, I’ve color coded the relationships. It’s also important to note that (\w+) means that you can place any character within there. So it basically means that if I type in a url with the pattern of:

http://domain.com/certificate/any character 1/any character 2/any character 3/any character 4/

the server will translate this to a real url that looks like this

http://domain.com/certificate.php?folder=any character 1&sort=any character 2&order=any character 3&page=any character 4




Conclusions

Save your new .htaccess file and place it into the web root folder on your server. But watch out yahoo web hosting users, because Yahoo Web Hosting has disabled this functionality. Those Bastards!!!

But the end result should leave you with a clean, easy to read url that search engines will looooooove to death. Except for Yahoo, Those Bastards!!!

Please share any Rewrite Engine tips if you have any, I’m sure mine is just 1 in a million.

digg it reddit stumble it facebook
Post to MySpace!
Add this to your blog:
(Copy & paste code)

2 Responses to “Cleaning Up Your URL Query String using Rewrite Engine”

  1. jack Says:

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^post/(\w+)/$ display.php?id=$1 [nc]

    would that work?

  2. admin Says:

    yes, that looks like it would work. Test it out, but make sure you are on an apache server. Also some hosting companies like yahoo, and godaddy wouldn’t allow the rewrite, or make it very difficult to rewrite. I suggest you avoid those two hosting companies. let, me know how the rewrite went for you.

Leave a Reply

Copyright © 2010 . All Rights Reserved . iCurious Media
Terms and Conditions . Privacy Policy . Site Map