From time to time I have seen a request like this:
“I want all the links from my Sitecore website to start redirecting from myhostname.com to www.myhostname.com.”
How do can this be done? There are actually a few ways you can do that. I’m showing you on this post only one which is easy and works just great.
Here are the 7 steps you can use to achieve that.
But before anything else we will need the very handy IIS Rewrite module in order for this to work. You can download it here.
There are two part that need to be properly configured:
- Sitecore’s site definition
- Create a rule using this module to take care of the redirection piece
Step 1 – Update the hostname attribute of the site definition to resolve both urls.
On your solution open the file SiteDefitition.config. By default it is located under App_Config/Include folder.
Ps: This assumes that you have removed the .example from the SiteDefinition.config. Also if you have a custom Sitedefinitions.config file this is where you should be updating this code.
Look for your site definition and update the hostName property of the site to resolve both to www.myhostname.com and myhostname.com. To do that you can either add both entries to the hostName separated by a “|” character or just use the “*” and let Sitecore handle the rest(most recommended).
Option 1: hostName=”myhostname.com|www.myhostname.com”
Option 2: hostName=”*myhostname.com”. This syntax means Sitecore will resolve anything that comes before myhostname.com, including “www.” which makes www.myhostname.com a hostname that it understands and resolves.
At this point Sitecore will resolve both urls myhostname.com and www.myhostname.com. But IIS won’t.
Step 2 – Locate your site definition on IIS and open it’s bindings
On IIS you need to have a binding that resolves www.myhostname.com for the site. To do that open your website on IIS and click Bindings as shown bellow:
Step 3 – Create a binding that resolves www.myhostname.com
Click the Add button and set the hostname as www.myhostname.com and click OK. You will see a window like this:
Step 4 – Open the URL Rewrite section of your site.
On the IIS section of your website definition look for the URL Rewrite button and double click it.
Step 5 – Click on Add Rules(s)
Step 6 – Create a Url Rewrite canonical url
On the Add Rule(s) window select the Canonical domain name section as shown bellow:
Step 7 – Select the primary host name
Make sure the hostname www.myhostname.com is selected. After that click OK.
That’s it! Now all the traffic on your Sitecore instance coming from myhostname.com will get redirected to www.myhostname.com