Redirect non-www to www

Hello,

Can you help me with this problem?

Recently we have implemented a web site using Sitecore SXA 9.0.1, and we are having problem to redirect our web site from https://domainname.com to https://www.domainname.com

I've created a new rule in CD Server web config file but still is not working as below, please note the first rule has been added by me.

 

<rewrite>
<rules>
<rule name="Redirect to www">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" redirectType="Permanent"/>
</rule>
<rule name="Root Hit Force HTTPS Redirection" enabled="true" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_METHOD}" pattern="GET" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/" redirectType="Permanent" />
</rule>
<rule name="Sitecore Login or Admin Force HTTPS Redirection" enabled="true" stopProcessing="true">
<match url="^(sitecore/(login|admin))$" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_METHOD}" pattern="GET" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="Forbidden" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_METHOD}" pattern="GET" />
</conditions>
<action type="CustomResponse" statusCode="403" subStatusCode="0" />
</rule>
</rules>
</rewrite>

 

Can you please advise?

 

Thanks advance for your help.

  • For this rewrite rule you need to have
    1) The URL rewrite module installed it is an extension to IIS
    2) You also need a IIS binding for both hostnames, (port 80 and https 443)

    beside in your rule you redirect to http, instead of https
    I assume you want https because looks like the next rule try to do that.

    And review your regex, looks like it now match host names start with www, if it is just for one hostname you can somethings like this:

    <add input="{HTTP_HOST}" pattern="^domain\.com$" />

    Than you get somethings like this:

    <rules>
        <rule name="Redirect to www">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^domain\.com$" />
          </conditions>
          <action type="Redirect" 
            url="{MapProtocol:{HTTPS}}://www.domain.com/{R:1}" />
        </rule>
     </rules>

    Or more generic use a ?!  , regex negative:

    <add input="{HTTP_HOST}" pattern="^(?!www.)(.+)" />
  • In reply to jan bluemink:

    Thanks for your comments.

    yes, I am looking redirect to https. It is would be like below?

    <rule name="Redirect to www">
    <match url=".*" />
    <conditions logicalGrouping="MatchAny">
    <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" negate="true" />
    </conditions>
    <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" redirectType="Permanent"/>
    </rule>

    I have hosted my web site in Azure, I created the custom domain for both: https://domain.com and https://www.domain.com

    Thanks in advance for your help.
  • In reply to Donaldo Tevez Portillo:

    I updated the answer looks like you regex pattern is for redirecting www to non-www. instead of "Redirect to www"

  • In reply to jan bluemink:

    Thanks I appreciate your help.

    I made the changes but still is not working.

    I guess, the app service need to be restart, do you think?

    I will do that ousite of business hours tonight.
  • After adding your rewrite rule you will need to touch the web.config or restart the app. It is nice to have this in a list of conditions in the event there is something you do not want to redirect to www for. Below is an example of this.

      <rule name="Redirect to WWW">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAny">
          <add input="{HTTP_HOST}" pattern="^domainname\.com$" />
        </conditions>
        <action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
      </rule>