Redirect domain to www prefix and vice versa

When building a website or blog, you need to decide how people will link to you. Either with www prefix or without www. Some peoples love to have www in front of their domain name but some are don’t.

Both of the them does not have pro or con. But it is better for you to choose one and stick to it. This is because we don’t want to get penalised by Google for content duplicates.

no-WWW to WWW

As for myself, I am using with www prefix for my domain. I feel it is more professional to have www in front of my domain but some don’t think like that. To redirect your website from non www to www prefix, you need to put these lines in your .htaccess file.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^$
RewriteRule ^(.*)$$1 [R=301,L]

As you can see here, the RewriteCond is to check the url. If the url is which is the condition is true, it will redirect you to www prefix like this

WWW to no-WWW

But if you prefer without www prefix, just twist the above code like this.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^$
RewriteRule ^(.*)$$1 [R=301,L]

BTW make sure to replace my domain with yours or it will not work. Hope this helps!

Extra Tip: Using WordPress? There is a Wodpress plugin that will automatically do this job. You just need to install the plugin and activate. It is very easy!


  1. This is what happen to and last time. but this has been fixed now.

  2. Sweet. Did the trick.

  3. scorpio2k2 says:

    thanks. verry useful