View Full Version : How to: Create friendly URLs with your .httaccess File!

04-15-2013, 05:43 PM
An example of SEO friendly URL can be seen in this page. Compare it with urls like http://adminspoint.com/?t=34 , you will find it easier to remember and the it also clearly tells you what this page is about. Furthermore, the words in the URL might match the search keywords, bringing more traffic from search engines.

If you are running a WordPress blog then you most likely will not need to edit your .httaccess File to make it URLs SEO Friendly as WordPress already has plenty of SEO Friendly features and plugins.

1. Creating .htaccess file

If you don't have a .httaccess file already created then create one and add the following lines if you already haven't.

RewriteEngine On

The first line Options +FollowSymLinks is required for some server configurations.

2. Create your own rewrite rule

For example, if you want to change links like www.adminspoint.com/showthread.php?t=2279 to http://www.adminspoint.com/t/2279/ OR. http://www.adminspoint.com/showthread.php?topic=random-talk to http://www.adminspoint.com/topic/random-talk/, here is the rewrite rule:

RewriteEngine On
RewriteRule ^topic/([a-zA-Z0-9]+)/$ index.php?topic=$1

Like regular expressions, the [a-zA-Z0-9] matches lower and uppercase of alphabets and numbers.

The asterisk inside the brackets + is a quantifier that match 1 occurence to infinite occurences.

Combining them, ([a-zA-Z0-9]+) matches alphanumerics of at least 1 character.

The caret ^ means “start with”, meaning the URL starts with the word “topic”.

The dollar sign $ means “end”, meaning the URL ends with a slash.

The $1 is backreference, it carries the content of the first group of brackets.

In other words, when user enters http://mysite.com/topic/faqs/ , the page being called and run would be http://mysite.com/index.php?topic=faqs

Example 2

If you want to change URLs like http://mysite.com/index.php?product=productname&price=30 to http://mysite.com/products/productname/30/. Basically its similar to above:

RewriteEngine On
RewriteRule ^products/([a-zA-Z]+)/([0-9]+)/$ index.php?product=$1&price=$2

The [0-9] in matches numbers only.
The plus sign is a quantifier that match 1 or more occurences.
Combining them, ([0-9]+) means 1 or more numbers.
Similarly, $1 will be the first brackets : product name and $2 would be the second brackets : price.

Example 3

If you want to change URLs like http://mysite.com/article.php?id=45 to http://mysite.com/article-45.html, here is how:

RewriteEngine On
RewriteRule ^article-([0-9]+)\.html$ article.php?id=$1

Alternatively, if you are not able to do these things on your own there are a few online Online URL Rewritters, which will make everything easier and simpler for you:
Static URLs are known to be better than Dynamic URLs because of a number of reasons

www.generateit.net/mod-rewrite/ (Best site)

1. Static URLs typically Rank better in Search Engines.
2. Search Engines are known to index the content of dynamic pages a lot slower compared to static pages.
3. Static URLs are always more friendlier looking to the End Users.

Example of a dynamic URL


This tool helps you convert dynamic URLs into static looking html URLs.
Example of the above dynamic URL Re-written using this tool

You would need to create a file called ".htaccess" and paste the code generated into it, Once you have created the .htacess file simply copy it into your web directory.
URL rewriting of this type would work ONLY if you are hosted on a Linux Server.

This tutorial was not fully written by me.