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:
php_and_mod_rewrite.php

but you want it to look like:
php-and-mod-rewite

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:
http://www.mywebsite.com/php-and-mod-rewrite

and not like:
http://www.mywebsite.com/php_and_mod_rewrite.php

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

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