Writing SEO Friendly URLs using .htaccess on Virtual Machine

A friendly URL accurately describes the page using keywords that are easy to read for both search engines and users. The URL is also shown in the search results and keep this point in mind i.e well crafted URLs are more informative and attractive to users and this means higher Click Through Rates.

It's much simpler task you need to maintain your URL much small and with good keyword as a parameter so that the page will be indexed well by the search engines.

User will come to know what informations will be present under such url.

So use the below sample example code to configure seo friendly url:

Normal practice of using url:

1) http://abcd.in/user.php?u=123

2) http://abcd.in/user.php?u=123&cat=php

3) http://abcd.in/page.php

Our major work is to rewrite it as:

1) http://sforsuresh.in/user/123

2) http://sforsuresh.in/user/123/php

3) http://sforsuresh.in/page

This can be achieved by creating new htaccess file and add these lines:

RewriteEngine On
RewriteRule ^user/([0-9]+) user.php?u=$1 [NC, L]
RewriteRule ^user/([0-9]+)/([0-9a-zA-Z]+) user.php?u=$1&cat=$2 [NC, L]
rewirteRule ^page  page.php

Let us ellaborate above code,

RewriteEngine: To enable we need to enable rewrite engine on.

RewriteRule: The RewriteRule directive is the real rewriting of rules that need to be executed.

$1 and $2 : Are variable used to represent dynamic values which we are pass in url against id and category.First ([0-9]+) represent $1 and second ([0-9a-zA-Z]+) represent $2 (from left to right) and so on.

NC: Non case sensitive pattern match.

L : It will stop the rewriting process immediately and you should not apply any more rules after this.

RewriteBase : It specifies the URL prefix to be used. If your project inside some folder say myproject than you can write like this:

RewriteBase /myproject/

RewriteCond : defines a rule condition.One or more RewriteCond can precede a RewriteRule directive.