Sitecore – 7 steps on how to enforce a website’s url from domain.com to www.domain.com

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:

  1. Sitecore’s site definition
  2. 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:

b1_step1

 

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:

b1_step2

 

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)

b1_step3

Step 6 – Create a Url Rewrite canonical url

On the Add Rule(s) window select the Canonical domain name section as shown bellow:

b1_step4

Step 7 – Select the primary host name

Make sure the hostname www.myhostname.com is selected. After that click OK.

b1_step5

 

That’s it! Now all the traffic on your Sitecore instance coming from myhostname.com will get redirected to www.myhostname.com

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s