Tuesday 5 June 2012

URL Rewriting and SEO

What is URL rewriting?

Most dynamic websites uses variables in the URLs to pass values like: http://www.mywebsite.com/myproducts.php?id=1, which is not SEO friendly. But in the sense of SEO this URL can be change to http://www.mywebsite.com/products/7. This can be done by URL rewriting on .htaccess on Apache environement. To achieve this mod_rewrite module should be ON. IIS (Microsoft server) doesn't include this functionality but using add-ons it can be used. ISAPI_Rewrite can be used to mod_rewrite functionality in IIS environment.

1) Simple replacement:

http://www.mywebsite.com/product_details_and_info.php

RewriteEngine On # Turn on the rewriting engine
RewriteRule ^product-details/?$ product_detals_and_info.php [NC,L]

The above code will change URL below:

http://www.mywebsite.com/product-details/

2) Match pattern and replacement:

http://www.mywebsite.com/product_lists.php?pid=10

RewriteRule ^products/([0-9]+)/?$ product_lists.php?pid=$1 [NC,L]

http://www.mywebsite.com/products/10/

Here $1 will replace all number in pid (like pid=10) with pid number after products (like /products/10).

3) Regular Expression match:

Because this pattern match with regular expression(RE) so be careful with special characters of RE.

RewriteRule ^rssfeed.xml$ rssfeed.php [NC,L]

Above example will not only match rssfeed.xml but also will match rssfeed1xml, rssfeed-xml. So to avoid this use below code.

RewriteRule ^rssfeed\.xml$ rssfeed.php [NC,L]

Other escaping character lists are:

     . => Any character
     * => Zero or more of the preceding
     + => One or more of the preceding
     {} => Minimum to maximum quantifier
     ? => Ungreedy modifier
     ! => Negative Pattern OR At start of string
     ^ => Start of string, or negative if at the start of a range
     $ => End of string
     [] => Match any of contents
     - => Range if used between square brackets
     () => Group, backreferenced group
     | => Alternative, OR
     \ => The escape character itself

4) Content Moved

RewriteRule ^articles/?$ http://www.mywebsite.com/articles/ [R,NC,L]

Here R flag denotes that articles is moved temporarily. Either a substitution URL can be given or header will sent back a code 302, it means moved temporarily.

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

Here R=301 shows that content is moved permanentaly.

Other Flags:
     C => Chained with next rule
     CO=cookie => Set specified cookie
     E=var:value => Set environment variable var to value
     F => Forbidden - sends a 403 header to the user
     G => Gone means no longer exists
     H=handler => set handler
     L => Last - stop processing rules
     N => Next - continue processing rules
     NC => Case insensitive
     NE => Do not escape special URL characters in output
     NS => Ignore this rule if the request is a subrequest
     P => Proxy
     PT => Pass through ( when processing URLs with additional handlers, e.g., mod_alias)
     R => Temporary redirect to new URL
     R=301 => Permanent redirect to new URL
     QSA => Append query string from request to substituted URL
     S=x => Skip next x rules
     T=mime-type => Force specified mime type


2 comments:

  1. Last comment I have requested for URL rewriting. Thanks a lot for solving my problem. This information will help me a lot.

    Keep posting such kind of useful tips.

    ReplyDelete
  2. Nice article. Got some good knowledge in SEO. Thanks. Keep it up.

    ReplyDelete