Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

SEO friendly URLs with .htaccess

Saturday, February 23rd, 2013 by Servage

Are your Search Engine Optimizing your website? There are many things to consider when doing so.

  1. Have good and proper content, relevant to your products etc.
  2. Support it with relevant meta tags, like keywords, description etc.
  3. Implement SEO friendly URLs, which make it easier for search engines to index your content, but also makes your URLs look nicer to the human eye.

Here is how you can get going:

Example URLs

// Bad URL:

// Good URL

In the second example, you simply get the product title (or slug) from the URL, and search your database to provide the proper product page, instead of doing it by productId, like in the first example.

Getting virtual URLs to map to your PHP script with htaccess

Normally a browser HTTP request goes to a file, like to the descriptions.php in the bad URL example above. But with the smarter, SEO friendly URLs, you want all URLs to map to a specific file – say index.php – and then you can determine if the URL is valid or not. Store the following in your web roots .htaccess file:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1

Getting the route in PHP

In the example above, all URLs are sent on to the index.php scripts. Fetch the URL inside the index.php like this:

$route = $_SERVER['PHP_SELF'];
$route = substr($route, strlen('/index.php'));

Have fun creating SEO friendly URLs :-)

SEO friendly URLs with .htaccess, 3.3 out of 5 based on 7 ratings
Categories: Tips & Tricks

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.