Mod_Rewrite – Remove Query String From URLs

There are plenty of tutorials and articles showing how to use the apache mod_rewrite module to tidy up messy URLs can create nice, human readable, search engine friendly URLs. No problem there and if that is what you are looking for you wont find that here.

What you will have more trouble finding is a tutorial (other than this one right here) showing you how to remove a query string from a URL. I guess this is a less popular topic but… nevertheless, with something as cryptic as mod_rewrite and URL rewriting the seemingly simple can take an age without solid advice so this tutorial will cover the reasons why you may want to do this along with the simplest and cleanest of solutions.

Tidy Rewriting

If you are redesiging a site which previously had a bunch of URLs with a get string and you want to point them at a flat page or the site root you may find yourself with the problem that your rewrite appends the URL onto the rewrite.

So, say for instance we had around 100 different instances of this page with different text in the getvar variable: oldpage.php?getvar=lets-remove-this

We would attempt to rewrite that to the site root with a rule like: rewriterule ^oldpage\.php$ / [R=301,L]

Your attempts to rewrite oldpage.php?getvar=lets-remove-this to the homepage actually result in: /?getvar=lets-remove-this

Cleaning it up

The solution to resolve this problem is incredibly easy, so much so, you need just one character. How is it done? Ironically, the question mark is the answer. You simply have to amend your original rule by adding a question mark after the substitution so / becomes /?

rewriterule ^oldpage\.php$ /? [R=301,L]

This will now rewrite oldpage to the site root and clear up the query string so you have a nice, clean rewrite. The question is the answer!

Marcus Miller is the owner of Bowler Hat Solutions and provides a range of affordable website design and internet marketing solutions for all shapes and sizes of businesses throughout the UK.

Bowler Hat is currently promoting a new and powerful website product that combines consultation, strategy, website design, a content management system, search engine optimisation, search engine marketing and social media marketing for only £1000.00. Visit Bowler Hat Solutions for more information.

Random Posts

Comments are closed.

Accounting Services - Mens Fashion Online - Web Design Company - Thailand Web Design Company - Web Design Services
Teak Furniture Thailand - Sunbelt United - Huahin Land for Sale - Worldcup USB Flashdrives - Retro Footballshirts Wholesale - Replica Football Shirts


Copyright © 2010 Articles
E-Web Solutions Ltd professional service for web design bangkok, web design pattaya,
web site design thailand, seo bangkok, search engine optimization thailand, logo design, corporate branding,
shopping carts and online solutions in thailand.