how to redirect all non www traffic to www in .htaccess as well as web.config


The below post shows how to redirect all non www traffic to www. For e.g. from just to

.htaccess in linux

.htaccess in linux

In Cpanel or Linux servers , We can do this just by adding following rewrite rules on .htaccess

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

Dont forget to change the to your domain name


We can do this on IIS web server ( Windows) simply by adding the following code on web.config  inside <system.webserver> element

<rule name=”Redirect to www” patternSyntax=”Wildcard” stopProcessing=”true”>
<match url=”*” />
<add input=”{HTTP_HOST}” pattern=”” />
<action type=”Redirect” url=”{R:0}” redirectType=”Permanent”/>

I have tried this over many times and all the time its a success.

Post your comments