Wednesday, October 17, 2007

How to Customize Ugly Link Structure

By default normal PHP driven websites use web URLs which have question marks and lots of numbers in them. This situation will cause an ugly and hard-to-remember link. It will also blinds the search engine to index your page.

However, if you're using a linux hosting (including EasyPhp for windows) you actually have the ability to create a custom URL structure for your links. This can improve the aesthetics, usability, and forward-compatibility of your links. You can also hide your programming language, set the .php to .html, and many more.

Here I'll show you how to create a beautiful SEO friendly links using .htaccess.

First of all, you need to make sure that your hosting company has enabled mod_rewrite module. If you're using EasyPhp server, you just need to do some modifications to your Configuration » Apache file called httpd.conf. Find the word and mod_rewrite.c - uncomment both lines. Save and you're done with server setting. But if you're using xampp server, you may explore them yourself as I'm not really in to it.

After that, you need to create a .htaccess file in your root directory of your website. Note the dot(.) in front of the file name - it shows that the file is hidden in linux file system.

Open the file in your editor's mode (i.e: Notepad, Dreamweaver, cPanel Code Editor etc) and write this code to the file and save:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Note that from now on, all unknown physical links will lead to index.php. All you have to do now is to create a function that handles all the portions after the slash of the site root as a virtual link. If you're confused, let me show you an example:


Here, is your site root.

The words after the first slash, 'show', is your first portion, 'my' is your second portion, 'camera' the third, and '40d' the last. That's what I'm talking about. I guess you've got it by now.

Back to the function. I always use this function to tell the system where it has to go:

This one is to return the value for each portion:
function portion($int) {
  if ( !defined('URI') ) define('URI',$_SERVER['REQUEST_URI']);
  $uri = explode("/",URI);
  if ( !@$uri[$int] ) return false;
  else return $uri[$int];

And this one is an example on how to set them to go to a certain page:
if ( portion(1) == "show" ) {
  if ( portion(2) == "my" ) {
    if ( portion(3) == "camera" ) {
      if ( portion(4) == "canon" ) {
        if ( portion(5) == "40d" ) There goes your additional code to show that Canon 40D Camera!;

Note that all the ifs are for the example only. I don't do programming like that in the real scenario.

Now, you're free to customize your links. Remember, all the virtual links will be redirect to the index.php. So it's your creativity to structure them.

If for me, I always arrange the portions to a sequence like this (from the very first slash after the domain to the far right end):

  1. main action like display, process, add, update, delete, etc;

  2. type of view like list, detail, etc;

  3. the category ID - if needed;

  4. the sub-category ID - if needed;

  5. the item ID;

  6. any additional needs, and so on..

Anyway, that's just my idea. You may structure them the better way.

Have a good day!