Tidy up your URL by using mod_rewrite. Not only will this hide the query string, but it will make the URL look more presentable.
Friendly URL's are a very important thing to have when developing a dynamic content site that uses long query strings. A query string is something similar to a URL like you'll see on Ebay, Google and other big sites. If you're running a small to medium size website and you want to make your URL's a little easier to find you can do so via Apache's mod_rewrite by following this tutorial.
Prerequisites
You'll need to have Apache's mod_rewrite compiled into your Apache Web Server and you'll need to have access to setting some options up in your httpd.conf or at least have a server admin who's willing to do it for you. You can follow my tutorial for installing Apache web server on Linux here. For quick reference, the configure options for mod_rewrite are:
./configure
--enable-module=rewrite
--enable-shared=rewrite
I strongly urge you to carefully consider what other options you need to compile into your Apache installation. The above example is only for this one module.
Once you've compiled Apache to use mod_rewrite, you will need to setup the httpd.conf file. Here's how I set mine up for this particular situtation.
<Directory /www/htdocs/yoursite>
Options ExecCGI FollowSymLinks Includes MultiViews
</Directory>
Some of the above directory options may not be necessary for what you are trying to do. You'll have to decide if you need them. If you are having problems you can also try to use AllowOverride All to see if you can get it to work.
Planning
It's a very important idea that you plan how to rewrite you URL's because you don't want to go back and change the links over and over again. Once you've got a good steady map of how to rewrite your links, you can go ahead and modify a test script to see if everything worked. If your links work properly then you'll be able to move on and modify all existing links. Let's take a look at this example below:
Query String URL: http://yoursite.com/tutorials.php?req=tutorial&id=3&page=0
mod_rewrite URL: http://yoursite.com/tutorials/3/0.php
Doesn't that look much friendlier?
Once you've compiled Apache, edited the httpd.conf file and planned your URL rewriting, you're ready to setup your .htaccess file which maps out what to do with each URL. Here's an example of what to put in your .htaccess file. I'll explain how this works below.
RewriteEngine On
RewriteRule ^tutorials/(.*)/(.*).php /tutorials.php?req=tutorial&tut_id=$1&page=$2
In the example above we have two lines. The first line RewriteEngine On starts the mod_rewrite engine. The second line is what does all the work.
RewriteRule ^tutorials/(.*)/(.*).php /tutorials.php?req=tutorial&tut_id=$1&page=$2
Here you are starting a RewriteRule
RewriteRule ^tutorials/(.*)/(.*).php /tutorials.php?req=tutorial&tut_id=$1&page=$2
This is the start of a regex that allows you to create wildcards for the URL that your friendly URL. This allows us to use whatever we want between the (.*) and compare them to the second part of this rewrite rule.
RewriteRule ^tutorials/(.*)/(.*).php /tutorials.php?req=tutorial&tut_id=$1&page=$2
This is the final part of the rewrite rule that tells us how to map the friendly url in part 2 of the RewriteRule to the actual URL that our script was written for. mod_rewrite will translate part one to part 2 automatically.
This works by using regex. The first (.*) will be $1 and the second (.*) becomes $2 in the translation. You can do this as many times as you want for your URL.
Say for example we had this url: tutorials/13/0.php.
We'll be matching up /tutprials.php?req=tutorial&tut_id=13&page=0 by using mod_rewrite!
Believe it or not that's all that's really to mod_rewriting!
You can really get carried away with this by adding category titles to some of your rewrite URLs to make them even more search engine friendly. All you have to do is create another regex and don't use that variable in your translation.
Rewriting your URLs can be valuable. We took the time to do that for most of our URLs on this website and it makes things simpler for your visitors. I encourage you to try this out and see if you like the results. Also I would like to add that you can do this by exploding your URL's in php and you would not need Apache's mod_rewrite. A tutorial will come for this someday, but for now you can get started using this "Voodoo" tool for Apache Web Server.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Apache Articles
More By Eric Rosebrock
developerWorks - FREE Tools! |
Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base. FREE! Go There Now!
|
|
|
|
Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
|
|
|
|
CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP. FREE! Go There Now!
|
|
|
|
CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP. FREE! Go There Now!
|
|
|
|
When you create browser-based applications that display XML data feeds, you often need to code the data-retrieval mechanism and the user interface. Mozilla Firefox provides an infrastructure that frees you from these tasks, so you can concentrate on your application's functionality. Learn how to use Asynchronous JavaScript + XML (Ajax) to download XML data from a Web server, and discover how you can use Extensible Stylesheet Language Transformations (XSLT) to transform it dynamically into Firefox user-interface elements expressed in XML User Interface Language (XUL). You can apply these techniques to any application that uses XML data sources. FREE! Go There Now!
|
|
|
|
Download the Rational Application Developer (RAD) v7.5 open beta code and start developing applications for the JEE5 standard which features EJB3.0, JPA, JSF 1.2, JSP 2.1 and Servlet 2.5 standards. When you use this beta you will see how you can increase developer productivity for already existing applications with improved support for refactoring, as well as adding new features to existing applications. In addition, the beta provides tooling for JD Edwards, Oracle, SAP, Siebel and PeopleSoft to improve the developer productivity with these enterprise systems. FREE! Go There Now!
|
|
|
|
You might know that you can pull XML data into OpenOffice's spreadsheet program, Calc, but did you know that you can create a filter to make word-processing documents out of data stored as XML? This tutorial shows you how to use OpenOffice's import/export filters to open your XML data as though it's just a plain document. From there, users can edit the document much more naturally and then save it back to its native format. You can also use this feature to easily turn your documents into XML data. FREE! Go There Now!
|
|
|
|
Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
|
|
|
|
Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1. FREE! Go There Now!
|
|
|
|
Get a free trial download of IBM Lotus Forms V3.0 (formerly Workplace Forms), which provides a zero-footprint eForms solution to help you automate and move forms-based business processes off the desktop and onto the Web. With Lotus Forms, you can extend applications beyond the firewall by creating a single electronic form document ready for use in both thick and Web 2.0 thin client format. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |