0

Windows Server Web.Config 301 to WWW

If you are running your websites on IIS you will find that .htaccess files are redundant. To force all traffic to a specific subdomain ie. http://yourdomain.co.uk to http://www.yourdomain.co.uk you can use a web.config file.

The following rules in web.config will force the browser to redirect to www.domain.co.uk.

<?xml version="1.0" encoding="UTF-8"?>
 <configuration>
  <system.webServer> 
   <rewrite> 
    <rules>
     <rule name="Redirect to non-www" stopProcessing="true"> 
      <match url="(.*)" negate="false"></match> 
      <action type="Redirect" url="http://www.domain.co.uk/{R:1}">
      </action>
     <conditions>
      <add input="{HTTP_HOST}" pattern="^www\.domain\.co\.uk$" negate="true">
      </add> 
     </conditions>
    </rule>
   </configuration>

Note: Don’t forget to change yourdomain.co.uk to your actual domain name!

Matt

Leave a Reply