This post describes how to properly configure your hostName attributes to ensure cross-site links work properly when hosting multiple sites in Sitecore.

Untitled Document

The secret of Sitecore's ability to resolve multiple websites via different URLs is within the <sites> configuration of the web.config. This is where each site's URL is defined and which subsequently determines how internal links between sites are resolved.

When I refer to internal links, I mean links to other pages within Sitecore using the internal link dialogue wizard. When such a link exists, Sitecore's link manager runs and transforms the links into relative links based on the settings in the <sites> node of your web.config.

When Sitecore runs a published site, it attempts to match the hostname used to resolve the site with a configured "hostName" attribute in one of the sites configured under the <sites> node. It does this by:

  1. Evaluating the sites listed under the <sites> node from top to bottom (yes, order is important). 
  2. It loads the first site configuration where it finds a hostname match or the first site that does not have a hostname configured (you’ll see this behavior in Test 2 below)
  3. If it does not find a match, the "website" site is used. By default, it does not have a hostname configured. In other words, the "website" configuration is ultimately the default/fallback site configuration. This also means that any sites you configure below the "website" entry will effectively be ignored since the "website" node will always be used.

To see this in action, let's walk through an example. Our end goal is to ensure cross-site links reflect the URL for the respective site.

Here is the setup in Sitecore:

Sitecore configuration

Site 1:
Home page - /sitecore/content/Site1
Child page - /siteocre/content/Site1/AboutSite1
URL - http://site1.url
 
Site 2:
Home page - /sitecore/content/Site2
Child page - /sitecore/content/Site2/ContactSite2
URL - http://site2.url
 

For our example, we will test each site URL(as defined above), confirm that the URL resolves the correct home page, and then look at the links in the home page to see how Sitecore resolves them. Here are the links I’ve supplied on the home pages for both sites:

Site 1 home page links    Site 2 home page links
AboutSite1
Site2
ContactSite2
AboutSite1
Site1
Site1
ContactSite2

We will make Site1 our default/main site. That is to say, the "website" site configuration will be configured to return Site1.

TEST 1

To begin, we will not create a site configuration for Site2 and we will not add a hostName attribute to the "website" configuration. So what do those links look like when we do?


 

A. Do the site URLs Work?

Input URL Desired Page Content Item Resolved
Test Successful?
http://site1.url Site 1 Home /sitecore/content/Site1
http://site2.url Site 2 Home /sitecore/content/Site1

B. Are the on-page links generating the correct URLs?

Test Location: Site 1 Home Page (http://site1.url)

Link Clicked AboutSite1
Desired URL http://site1.url/AboutSite1.aspx
Actual URL http://site1.url/AboutSite1.aspx
Content Item Resolved /sitecore/content/Site1/AboutSite1
Test Successful?

Link Clicked Site2
Desired URL http://site2.url/
Actual URL http://site1.url/Site2.aspx
Content Item Resolved /sitecore/content/Site2
Test Successful?

Link Clicked ContactSite2
Desired URL http://site2.url/ContactSite2.aspx
Actual URL http://site1.url/Site2/ContactSite2.aspx
Content Item Resolved /sitecore/content/Site2/ContactSite2
Test Successful?

Test Location: Site 2 Home Page (http://site2.url)

Since the wrong home page was loaded – we cannot test the links on the site 2 home page

Observations

  1. Site URLs 
    • Since a unique site configuration for Site2 hasn't been configured, Sitecore loads the default "website" site configuration when the http://site2.url is used which results in the Site1 home page being loaded.

  2. On-Page Links 
    • Links to Site1 are as expected

    • Links to Site 2 - Sitecore is able to create links to these pages, even though the Site2 pages are not inside the Site1 node. However, the URLs are incorrect 

Conclusion

All URLs will resolve the default "website" site configuration if other sites are not configured.
 

TEST 2

Let's add a site configuration for Site2 above the website configuration but for now, we won't configure the hostName attribute for our new site. What do the links do now?


A. Do the site URLs Work?

Input URL Desired Page Content Item Resolved
Test Successful?
http://site1.url Site 1 Home /sitecore/content/Site2
http://site2.url Site 2 Home /sitecore/content/Site1

B. Are the on-page links generating the correct URLs?

Test Location: Site 1 Home Page (http://site1.url)

Since the wrong home page was loaded – we cannot test the links on the site 1 home page

Test Location: Site 2 Home Page (http://site2.url)

Link Clicked AboutSite1
Desired URL http://site1.url/AboutSite1.aspx
Actual URL http://site2.url/AboutSite1.aspx
Content Item Resolved /sitecore/content/Site1/AboutSite1
Test Successful?

Link Clicked Site1
Desired URL http://site1.url/
Actual URL http://site2.url/Site1.aspx
Content Item Resolved /sitecore/content/Site1
Test Successful?

Link Clicked ContactSite2
Desired URL http:// site2.url/ContactSite2.aspx
Actual URL http://site2.url/Site2/ContactSite2.aspx
Content Item Resolved /sitecore/content/Site2/ContactSite2
Test Successful?

Observations

  1. As I described earlier, Sitecore evaluates the site nodes for the first match. Without a hostname attribute in place, Site2 becomes the first to match and is loaded. This means both http://site1.url and http://site2.url load the Site2 home page.

Conclusion:

Our Site2 configuration is now the default configuration. 

 

TEST 3

Let's add the hostName attribute to our new site: hostName="site2.url"

A. Do the site URLs Work?

Input URL Desired Page Content Item Resolved
Test Successful?
http://site1.url Site 1 Home /sitecore/content/Site1
http://site2.url Site 2 Home /sitecore/content/Site2

B. Are the on-page links generating the correct URLs?

Test Location: Site 1 Home Page (http://site1.url)

Link Clicked AboutSite1
Desired URL http://site1.url/AboutSite1.aspx
Actual URL http://site1.url/AboutSite1.aspx
Content Item Resolved /sitecore/content/Site1/AboutSite1
Test Successful?

Link Clicked Site2
Desired URL http://site2.url/
Actual URL http://site2.url/
Content Item Resolved /sitecore/content/Site2
Test Successful?

Link Clicked ContactSite2
Desired URL http:// site2.url/ContactSite2.aspx
Actual URL http:// site2.url/ContactSite2.aspx
Content Item Resolved /sitecore/content/Site2/ContactSite2
Test Successful?

Test Location: Site 2 Home Pge (http://site2.url)

Link Clicked AboutSite1
Desired URL http://site1.url/AboutSite1.aspx
Actual URL http://site2.url/AboutSite1.aspx
Content Item Resolved /sitecore/content/Site1/AboutSite1
Test Successful?

Link Clicked Site1
Desired URL http://site1.url/
Actual URL http://site2.url/Site1.aspx
Content Item Resolved /sitecore/content/Site1
Test Successful?

Link Clicked ContactSite2
Desired URL http:// site2.url/ContactSite2.aspx
Actual URL http://site2.url/Site2/ContactSite2.aspx
Content Item Resolved /sitecore/content/Site2/ContactSite2
Test Successful?

Observations

  1. We finally have 2 unique urls loading different sites in Sitecore. http://site1.url resolves the Site1 home page and http://site2.url resolves the Site2 home page.

  2. Site1 is aware of the the "site2.url" hostname, and appropriately applies it to urls linked to pages in Site2

  3. Site2 is not aware of the "site1.url" hostname. Links still reference links to Site1 pages via the "site2.url" hostname

Conclusion:

Sitecore understands that pages belonging to Site2 should be linked using the "site2.url" hostname 

TEST 4

We will now add the hostName attribute to the "website" site configuration: hostName="site1.url"

 

A. Do the site URLs Work?

Input URL Desired Page Content Item Resolved
Test Successful?
http://site1.url Site 1 Home /sitecore/content/Site1
http://site2.url Site 2 Home /sitecore/content/Site2

B. Are the on-page links generating the correct URLs?

Test Location: Site 1 Home Page (http://site1.url)

Link Clicked AboutSite1
Desired URL http://site1.url/AboutSite1.aspx
Actual URL http://site1.url/AboutSite1.aspx
Content Item Resolved /sitecore/content/Site1/AboutSite1
Test Successful?

Link Clicked Site2
Desired URL http://site2.url/
Actual URL http://site2.url/
Content Item Resolved /sitecore/content/Site2
Test Successful?

Link Clicked ContactSite2
Desired URL http:// site2.url/ContactSite2.aspx
Actual URL http://site2.url/ContactSite2.aspx
Content Item Resolved /sitecore/content/Site2/ContactSite2
Test Successful?

Test Location: Site 2 Home Pge (http://site2.url)

Link Clicked AboutSite1
Desired URL http://site1.url/AboutSite1.aspx
Actual URL http://site1.url/AboutSite1.aspx
Content Item Resolved /sitecore/content/Site1/AboutSite1
Test Successful?

Link Clicked Site1
Desired URL http://site1.url/
Actual URL http://site1.url/
Content Item Resolved /sitecore/content/Site2
Test Successful?

Link Clicked ContactSite2
Desired URL http:// site2.url/ContactSite2.aspx
Actual URL http:// site2.url/ContactSite2.aspx
Content Item Resolved /sitecore/content/Site2/ContactSite2
Test Successful?

Observations

  1. Cross-site links are now accurately reflecting the hostName attributes for both sites.

1. Cross-site links are now accurately reflecting the hostName attributes for both sites.  

Conclusion

The hostName attribute is very important and required to properly resolve cross-site links hosted in Sitecore.

Creative Commons photo courtesy of Nina Matthews Photography