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 teksupport.in to www.teksupport.in

.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} ^domain.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

Dont forget to change the domain.com 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

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

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

Post your comments

 

 

 

Leave a Reply

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