How to create SEO friendly URL using .htaccess ?

Note * To know about htaccess please follow this link.

You can perform several actions using htaccess . Among them we are going to discuss about the htaccess re-writing rule i.e SEO friendly URL .

 As we know URL plays the vital role in the search engine optimization , so seo friendly URL is most important part.

In most of the dynamic website you can see URL like

http://www.example.com/product.php?product_id=25

But by using the Rewrite rule via htaccess you can create clean URLs

http://www.example.com/product/25 

Re-writing these URLs makes your site more valuable. it is much hard to redirect someone without the actual links , thus the URL Re-writing gives you the shorter , much logical and clean URLs .

Also it has the power to give you the descriptive URLs. As for example instead of  ’25 ‘ you can also assign some meaningful URLs that actually describes the products.

http://www.example.com/product/php-books

These descriptive URLs are not understood by the server,so you need to define the rule for the URL re-writing via ‘,htaccess’ files to to let the server to what the URL you want for the pages or products.

At the initial you must let your server that you are re-writing the url as that is not default to the server , so you need to do write below line first .

RewriteEngine On 

Now the server knows that re-write engine is on and you can now define the re-write rule .

RewriteRule  ^products/?$  product.php  [NC,L] 

From the above re-write rule let’s discuss each parts individually.

RewriteRule : This tells the server that we are creating the new rule for re-writing URLs.
^products/?$  : This is the Pattern or the set of rules that tells the server what pattern your URL should look like . Each pattern must have some special characters and each spacial characters have some specific rules.

  • dot(.) – any characters.
  • asterisk(*) : zero or more 
  • plus (+) : one or more 
  • curly brakets {} : explains minimum too maximum quantifier
  • question mark ? : ungreedy modifier
  • exclamation mark ( ! ) : negative patern or at the start of the string
  • carat ( ^ ) :  start of string, or “negative” if at the start of a range
  • dollar sign ( $ ) : end of string
  • large bracket [] : matches any of the contents with in
  • dash ( – ) : range if used between the bracket
  • paranthesis () : group , back referenced group
  • pipe ( | ) : alternate , or 
  • backslashes ( ) : escape character itself 
product.php : it is known by the subsituation pattern , is the exact link that the server will understand .
[NC, L] : These are the flags which commands the server , how to implememt the rule . Here , NC commands the server that it is non-case sensitive and L commands the server not to process any other rules if it is applied .
# : Any rules of comments avaliable in the .htaccess files are commented by using hash ( # ) infront of the rule, comments or text.

So let’t try with the sample rule for URL re-writing.

RewriteEngine On RewriteRule ^products/([0-9]+)/?$ product.php?product_id=$1 [NC,L] # handle the product

The url re-writing have number of utilities , but some of the useful utilities are listed below .

In order to redirect from old domin to new domin .

RewriteCond %{HTTP_HOST} domain-old.com [NC] RewriteRule ^(.*)$ http://www.domain-new.com/$1 [L,R=301] 

In order to redirect the site to www from non www

RewriteCond %{HTTP_HOST} ^domain.com [NC] RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301] 

In order to redirect the site to www from non www.

RewriteCond %{HTTP_HOST} ^www.domain.com [NC] RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301] 

In order to redirect from old page to new page with in the same site .

RewriteRule ^old-url.htm$ http://www.domain.com/new-url.htm [NC,R=301,L]

Leave a Reply

Your email address will not be published.Required fields are marked *