Check out the Latest Articles:

Nearly 80% of the websites that I visit these days are dynamic. Thanks to the Server Side Scripting technologies we now don’t actually need to create a new html file for each page of content for your website. Content Management Systems has a rising popularty these days for developing websites.

Generally the scripts can get you dynamic data on the fly by passing some parameters in the URL. Like for example http://www.mygeekpal.com/?p=171 can get you the third page of some article on a website. However URL structure like this is not to the point for Search Engine Optimization. The concept of Pretty URLs evolves here.

Pretty URL is a  10 year old SEO Technique where you can stuff the keywords related to the content on the page into the URL for better SEO Rankings. Generally we stuff the title of the content to make it more relevant. For example you can see the URL of this page http://www.mygeekpal.com/171/how-to-create-pretty-urls-using-htaccess/ where instead of the old ugly URL which shows the post number, the current URL shows the title of the post. This feature is called Permalinks or Permalink Structure in WordPress.

How to create Pretty URLs

The concept of creating Pretty URLs is very simple. URL Rewriting is a very powerful feature support by apache which allows you to change the way URLs look and point to some content. For example a page with the URL http://example.com/search/cat/ can easily point to http://example.com/?search=cat with no trouble. URL Rewriting is basically done using a htaccess file and php script.

Let us see step by step examples of creating Pretty URLs

Firstly you need to have a .htaccess file in your root folder. This is the primary file which basically points the rewritten URL structure. Now open up the .htaccess file with any text editor and add these lines of code on the top.

Options +FollowSymLinks
RewriteEngine On

FollowSymLinks is a security requirement of the Apache rewrite engine. It is required to be on, and in most of the times it is already set as on. RewriteEngine On is cleanly evident that we are telling Apache to activate the rewrite engine so we can use it.

Syntax for rewriting a URL
RewriteRule PATTERN DESTINATION FLAGS

RewriteRule is a keyword for rewriting a URL where pattern is the pattern of the URL that is entered in the browser and Destination is where the URL should point to. Flags are simply some optional parameters that alter the behavior of the rewrite rule. Flags are put inside square braces [] and the most common flags are

  • NC – Means No-Case, specifies that the URL to rewritten is case insensitive
  • L – Last, means that if current rule is applied then do not apply any more rules and end URL rewriting
  • R – means redirect the URL using header parameters instead of transparently rewriting it

To redirect http://example.com/cat.html to http://example.com/?view=cat
Rewriterule ^cat\.html$ ?view=cat [L,NC]
Coming to some complex URL Structure where you want to redirect multiple URLs in some formatted structure like you want to redirect

  • http://example.com/topic-something.html to http://example.com/?topic=something
  • http://example.com/topic-dog.html to http://example.com/?topic=dog
  • http://example.com/topic-house.html to http://example.com/?topic=house
  • http://example.com/topic-internet.html to http://example.com/?topic=internet

Rewriterule ^topic-([a-z0-9_-])\.html$ ?topic=$1 [L,NC]

The above ways to redirect are quite simple. But if you want something more complex like the current page’s URL to be redirected, then first we rewrite all the URLs to a single URl, say index.php and then we output the content based on what the actual URL parameters are. TO do this first add this to the .htaccess file

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule .* index.php [L]

And for the sake of this example, the mockup code for the index.php will be as follows. Please note that the code is very insecure and just for the sake of this example. You should never implement code like this.

<?php
$uri_request = empty($_SERVER['REQUEST_URI']) ? false : $_SERVER['REQUEST_URI'];
switch ( $uri_request ) {
	case '/web-20':
		include 'internet/web-20.php';
		break;
	case '/pes2009':
		include 'games.php?game=pes2009';
	default:
		include '404.php';
}

?>

Related posts:

  1. 301 Redirect: The Search Engine Friendly Way To
  2. Handling Traffic With Custom 404 Error Pages
  3. How to prevent spam comments using .htaccess file
  4. How to Create a WordPress Plugin – Hello World!
  5. MS DOS Tutorials – How to Create, Delete, Change, Rename Directory and Files


  1. krishna on Monday 20, 2009

    Great pal. I enjoyed this simple and clean explanation of URL rewriting concept.

  2. Atif on Monday 20, 2009

    thanks, I am glad you liked it.