Making Friendly URLs
Posted on August 29, 2008, Filled under PHP,
Bookmark it
The aim of this tutorial is to show you how to make SEO Friendly URLs. First of all you must check if your Apache Server has the mod_rewrite module enabled.
Too many parameters in an URL can create problems in crawling for a Search Engine. A friendly optimised URL would always be a better alternative if this option is available to you. These URLs are better indexed by Search Engines and people are more likely to click on static links which contain a description of what the link is about.
The rewriting rules should be written in the .htaccess file that must be created and uploaded in the main root of your site.
Here’s an example of a dynamic URL:
http://www.yourdomain.com/?c=5&p=20
A better (SEO) alternative of this URL would be:
http://www.yourdomain.com/articles/5/20-seo-techniques.html
You can obviously notice what this URL is about.
The server sends a request to the .htaccess file passing the category id and the post id.
The .htaccess file will look like this:
# Enable mod_rewrite RewriteEngine On # Sets the base URL for per-directory rewrites RewriteBase / RewriteRule ^articles/([0-9]+)/([0-9]+)-seo-techniques.html$ /?c=$1&p=$2
We use regular expressions to tell the script that only numbers values should be parsed. The static url is automatically converted to the dynamic one.
Here are more examples of rewrite rules:
RewriteRule ^terms-and-conditions.html$ /index.php?module=terms # example: http://www.mydomain.com/category/10/post/35/php-functions.html RewriteRule ^category/([0-9]+)/post/([0-9]+)/(.*).html$ /?c_id=$1&p_id=$2
By using this rewrite techniques your URLs are better indexed by search engines and are more likely to have a higher rank.
