web.config redirect non-www to www

Total Post:106

Points:748
IIS 7  IIS 
 449  View(s)
Ratings:
Rate this:

I want to redirect non-www urls to www url for both http and https . I tried following rules in web.config but not working. 

please help

 <rewrite>
    <rules>
      <!-- For force ssl -->
      <rule name="Redirect to WWW" 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>
      <!-- For force ssl -->
      <!-- For force www -->
      <rule name="Redirect to WWW https" stopProcessing="true">
        <match url=".*" />
        <conditions>
          <add input="{HTTPS}" pattern="^domain.com$" />
        </conditions>
        <action type="Redirect" url="https://www.domain.com/{R:0}" redirectType="Permanent" />
      </rule>
      <!-- For force www -->
    </rules>
  </rewrite>
  <!-- For force ssl and www -->

  1. Post:282

    Points:2008
    Re: web.config redirect non-www to www

    In the web.config inside <system.webServer> code here.... </system.webServer> section of your web.config add the following lines of:

    <rewrite>
      <rules>
    <!--for http to https-->
      <rule name="http to https" stopProcessing="true">      <match url="(.*)" />      <conditions>      <add input="{HTTPS}" pattern="^OFF$" />      </conditions>      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />   </rule>
    <!--for non www to www-->
      <rule name="non www 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}" />       </rule>
        </rules>   </rewrite>

    i hope it will help to you.

      Modified On Jun-15-2018 05:27:29 AM

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.