Add to Favorites

Url rewriting

You may frequent sites who make use of url rewriting to create fancier and cleaner urls. Many websites these days are driven dynamically by database applications instead of coded in html by hand with static files. This means that the url you are accessing may or may not have a file by that name sitting behind the server on the hard drive. Just because you access a domain with domain.com/sandwich.html doesn't mean that there is really a file called sandwich.html on the server. This is the beauty and inherent confusion for people that come across url rewriting.

A good example of URL rewriting in action is a content management system. Most content management systems will maintain all the pages of a website in a database that allows an administrator to go in and edit the website without directly accessing the server files or programming html. Content management systems that store their pages in a database need a way to get them back out and display them to users when someone visits the website. It is most likely that each page has a unique id associated with it, and you have come across websites that have a url like domain.com/index.php?page_id=2 . These sort of urls can be confusing and don't really mean anything to a human being. That url could be calling the content management system asking for the page content for a page with the id 2 and then displaying it to you.

A cleaner url would make use of rewrites, the administrator would define the url he wants for the page in the content management system, then you would pull up the page as domain.com/sandwich.html instead of domain.com/index.php?page_id=2.

The driving component behind this sort of url clean up, is a url rewrite. A url rewrite will tell the server to look for a file or direct the user to a different page based on the page they requested. In this case we would make a rewrite that tells the server that if the file does not exist ( in this case sandwich.html does not exist ) then to call the content management system for the page sandwich.html.

At this point the server hands off control to the application, the content management system finds the page for sandwich.html and then returns that page's contents to the user; in the case that a user may request burger.html and the content management system does not have a page for burger.html, the content management system would then be responsible for informing the user that the page he requested was not found.

For those of you who are more technical in nature and looking for more information about how to actually construct the syntax for a url rewrite, stay tuned for next week.

Comments

Be the first to leave a comment on this post.

Leave a comment

To leave a comment, please log in / sign up