Forwarding from non-www to www domain using URL rewrite rules in Apache

Posted by in Code Snippets, Technology

Problem Description:
With the installation of WordPress multisite network in my site, entering the URL http://sufinawaz.com took me to the signup page whereas entering http://www.sufinawaz.com took me to the homepage as it should! I wanted http://sufinawaz.com to redirect to http://www.sufinawaz.com and http://sufinawaz.com/blog to http://www.sufinawaz.com/blog.

Solution:
I put the following block of codes right under the ruleĀ RewriteEngine On in .htaccess file and everything is a smooth sailing here onwards!

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

PS: 301 is a search engine friendly HTTP redirect.