How redirect non-www to www (CD Server)

Hi

I hope you can help me.

Recently we implemented a new web site in SXA 9.0.1, and we are having a problem with the redirect, by default the web site is getting https://domainname.com, we are trying to set up the redirect to https://www.domainname.com.

 

We've added the below rule in the web config in our CD Server.

 

<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>

</rewrite>

 

But sill is not working. 

 

Can you please advise how can we resolve it?

 

I appreciate in advance your help.

 

Thanks.

  • Hello Donaldo,
    Is it on premises installation or Azure PAAS?
    If on premises please install this www.iis.net/.../url-rewrite you can install from "Microsoft Web Platform Installer" as well.
    If Azure PAAS it should work with re-write rules.
    Thanks,
    Ketan Garala
  • Hey Donaldo, the first thing I see in your rewrite rule is that you are redirecting to HTTP, instead of HTTPS. If you want to enforce SSL, you should make sure to have the correct protocol in your redirect.

    My regex skills are rusty, but you may want to test your pattern with a hardcoded pattern match like "domainname.com" to make sure it isn't the regex format. If it works without the regex, then at least you will have identified that the problem is with the pattern.

    You may want to have "appendQueryString=true" on your action as well, in case you have any forms or campaign codes, etc. that would have "?something=somethingelse" in the URL. You usually want to preserve those on the redirect.

    One final note, remember that browsers will cache permanent redirects. While you are testing, you might want to make this a temporary redirect until you get it working, then switch to permanent. Otherwise, you will need to clear the browser cache of redirects to test any changes you make.