SEO Friendly URLS with .htaccess

Search engine optimization, or short, seo, means optimization so search engines will rank your site higher in their views. This will affect your visitors, in a positive way. What is very important for web shops, because more visitors, is more sales. Even sites with advertising will benefit from more visitors.

When we talk about seo friendly url’s, we ask ourselves the question which link will receive the most visitors:

  • http://example.com/index.php?content=article&sub=7
  • http://example.com/article/7/Title

Some articles say that easy readable urls only affect your visibility for search engines really small. But when a user has to choose between these urls, after searching for something, which link would he choose, do you think?

Open source web applications

Cms (content management systems) like joomla, drupal or wordpress, embed this possibility by default. Sometimes you just only have to activate it. If you use a cms, please do use these functions!

Self-made applications

.htaccess

Just like joomla, drupal and wordpress does, the easiest way to make the friendly urls possible, is to use a .htaccess file. (Note: This only works on the apache server)

This file makes it possible to set additional settings, you could have done in the apache configuration file to. But because most web hosting companies do not allow to edit that file, .htaccess files are really useful!

To create an .htaccess file, we advise to use the build in notepad editor from Windows. Just create a new file and save it as htaccess.txt (Because you can’t name it .htaccess in Windows). After completion of the file, you can upload it with your ftp client, of web ftp interface, and rename it to .htaccess!

These file has to be in your root from your site. An example of the directory (how it could be):

  • Pictures
  • Css
  • Files
  • .htaccess
  • index.php
  • info.php
  • contact.php

RewriteEngine

This is the built-in function from apache, who allows you to modify urls typed in the browser, and sending the right page back. Best of all, you can use this in your .htaccess!

Imagine you have the following .php pages:

  • pics.php
  • groups.php
  • contact.php

Wouldn’t it be nice to access these files with the following urls?

  • http://example.com/pics
  • http://example.com/groups
  • http://example.com/contact

Example of the .htaccess file to accomplish this.

Options +FollowSymLinks
RewriteEngine on
# Checks if the requested url path exists with .php, and if so, it allows the RewriteRule to do so.
RewriteCond %{REQUEST_FILENAME}.php -f
# Rewrite to append “.php” to extensionless URL-path
RewriteRule ^(([^/]+/)*[^.]+)$ $1.php [L]

This is possibly the easiest way to exlude the .php from your url.

What also is very common is the following: : http://example.com/info/ or http://example.com/blog/5/Title
The following example makes this possible.

RewriteEngine On
RewriteRule ^(.*)/(.*)/(.*) index.php?page=$1&sub=$2&$3

This example makes also the following url possible: http://example.com/forum/common/topic=7&active=3. This link will search for the following file: index.php?page=forum&sub=algemeen&topic=7&active=3.

The last example is a bit more complex, and could be an example for a download site.

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^files/([^/]+)/([^/]+).rar /download.php?cat=$1&file=$2 [NC]

This link (http://exaple.com/files/browsers/firefox) would reffer to donwload.php?cat=browsers&file=firefox.

Do not forget to addapt your links!

All the links you have, in your site, menu, … you have to modify! Remove the old links, and add the new ones, who do not have an extension, or are like files/browsers/firefox.

Otherwise, creating the .htaccess for seo reasons, is really stupid, because yahoo or google will think that it is duplicate content.

Images and css files

It might occur that suddenly your images are gone, or your css style. This is because for those links, (image links or css links) the RewriteEngine also tries to modify the links.

Lucky for us there is a simple way to block the RewriteEngine for folders where you put your images, css files or file folders. Just put the following RewriteCond just under RewriteEngine On

RewriteCond %{REQUEST_URI} !^/images/

Just add all the folders, and the job is done! You now have a much easier and more human readable site!

Sources

Thanks to the following sources:

http://httpd.apache.org/docs/1.3/howto/htaccess.html