A small little gotcha came by today as I was working on a multi-site Sitecore solution for sites on multiple domains. I saw a question on Stack Overflow which is in fact a question I needed to know the answer to myself. It had to do with binding a site on both a subdomain and no subdomain for the same Sitecore site of a multi-site solution.
<site ... /> nodes have an optional
hostName attribute to bind that specific site to match on the provided host. This is a filter to determine which site to put in context based on the current request’s hostname. If none match, the last site defined (typically left as the main “website”) will be put in context. The question was about handling both a “www.site.com” host and “site.com” and how the typical
hostName="*.site.com" doesn’t handle that (because it only handles any subdomains). Needing to know the answer myself I went to a good friend and fellow Sitecore developer Mark Stiles. Mark has significant experience on large scale multi-site installs so he immediately fired back a simple answer to “remove the dot.” Thus, we get
hostName="*site.com". Simple, and it works, since it catches anything before the actual hostname, whether that includes a subdomain and dot or not. So if you ever need to handle any subdomain or none, change your hostName filter to not include a dot. You could alternatively pipe-delimit both the non-subdomain and the catch all subdomain.