August 15, 2010 in Search Engine Optimization, Tutorials by admin

Many people have a really hard time finding a way to generate clean links for their site. Everyone wants to increase their search engine optimization and this is one of the absolute best ways to do it. In this example we will remove the .php from our file name and then turn all underscores into dashes.

You are probably asking why would you want to change underscores to dashes. The answer is actually very simple. Google and like search engines read dashes as spaces which separates the words, whereas underscores are treated like an underscore and read as one word.

Lets say you have a file named:

but you want it to look like:

How would we go about doing this?
We will be using our .htaccess file located in the root of your website and in any sub folder you wish to run it in. Using mod_rewrite we will be able to do all of this and in our html we can create our links like this:

and not like:

Save this code below in a text file and name it .htaccess to change your php files into seo friendly urls.

RewriteEngine On
RewriteRule ^([\w]+)/?$ $1.php
RewriteRule ^([^-]+)-([^-]+-.+)$ /$1_$2 [N]
RewriteRule ^([^-]+)-(.+)$ /$1_$2 [L]