What is this? From this page you can use the Social Web links to save Friendly URLs, the easy way to a social bookmarking site, or the E-mail form to send a link via e-mail.

Social Web

E-mail

E-mail It
June 30, 2008

Friendly URLs, the easy way

Posted in: Programming,SEO, SEM

If you ever wonder how to create those cool links to your pages, here’s a short tutorial…

Those “old” links like http://www.example.com/index.php?p=1&s=2&t=ok are history, the new look-and-feel is http://www.example.com/news/technology/.

There are number of advanteges with using this friendly URLs:

  • visitors can easily see where is their current location within your website
  • easy to remember direct links to a certain page on your website
  • Google “reads” your website more accurate and gives a better search results, so this is also a very good SEO technique (a must-have one actually)

I’ll show you an implementation of friendly URLs using XAMPP (my favorite “PHP server package” that I would recommend). XAMPP is an easy installation package that combines Apache+PHP+MySQL and even some other modules, and it works great.

First step is to check that Apache web server loaded a module that is responsible for handling URL rewrite.
Open the “C:\xampp\apache\conf\httpd.conf” file and search for “mod_rewrite.so” and check that the line is uncommented.

Then you need to create a new directory alias in Apache web server. If you’re using XAMPP the location of configuration file is at the following location: C:\xampp\apache\conf\extra\httpd-xampp.conf

Open that file in your favorite text editor and add something like:

  1.     Alias /example "C:/xampp/htdocs/www.example.com/"
  2.     <Directory "C:/xampp/htdocs/www.example.com">
  3.         Options +FollowSymLinks
  4.         AllowOverride All
  5.         Order allow,deny
  6.         Allow from all
  7.     </Directory>

where “C:/xampp/htdocs/www.example.com/” is the location to your website files and /example is a directory alias you just created.

Now you need to restart the Apache.

The previous step was needed to setup the usage of .HTACCESS file. Here you can find more information on .HTACCESS file usage, but just for now, as a beginner, all you have to now is that .HTACCESS file is a on-the-fly Apache configuration for your website that is placed at the root of your website.

Next step is to create .HTACCESS file and set it’s “actions”.

In the root folder of your website (“C:/xampp/htdocs/www.example.com/”) create a new text file with .HTACCESS filename and write the following instructions into it:

  1. RewriteEngine on
  2. RewriteBase /example
  3.  
  4. RewriteRule ^news/$ index.php?page=news

Just for testing purposes, create another new text file with INDEX.PHP filename and write the following code into it:

  1. <?php
  2. echo $_GET[‘page’];
  3. ?>

Save the file and point your web browser to: http://localhost/example/news/

As a result you should see a “news” text printed out in the web page.

What we actually did is a simple URL rewriting, or you might say a transparent (for visitor) URL redirection from one URL to another. With our .HTACCESS instruction Apache “transformed” typed-in URL into a INDEX.PHP call with one variable sent using GET method (“?page=news”).

That’s it. This is a rather simple example, and if you don’t have many pages you might think of using this method for friendly looking URLs on your website, but on a larger website you might wanna do some more complicated stuff, like this:

  1. RewriteEngine on
  2. RewriteBase /example
  3.  
  4. RewriteRule ^news/(0-9A-Za-z_-]+)$ news.php?topic=$1

The create a new NEWS.PHP file:

  1. <?php
  2. echo $_GET[‘topic’];
  3. ?>

And point your web browser to http://localhost/example/news/technology/

Then point your web browser to http://localhost/example/news/politics/

The previous example is using some RegExp instructions to make this subsection of a website dynamic. This way you can handle subsections without the need to write one line in .HTACCESS for each of them. But, be very carefull how you handle this dynamic URLs as they are potential security risk!

There is another nice usage of this URL rewriting method – point your old links to a new one. If your don’t want to lose visitors that are coming from Google search results to your old pages that don’t exists anymore, you should point them to a new address, and here is how:

  1. RewriteEngine on
  2. RewriteBase /example
  3.  
  4. RewriteRule ^old-link.html$ new-link.html

 


Return to: Friendly URLs, the easy way