Normally you already specified the hostname in the site definition configuration file.
You can get the hostname by calling
Sitecore.Context.Site.HostName
or Sitecore.Context.Site.TargetHostName
.
if you have a list of sites domain delimited by |
, it will return you all the values from the hostName
attribute.
Then you can split by |
to get the different sites URL. If you specify the targetHostName attribute as https://www.mysite.com, then you can make use of the TargetHostName.
Proposed Solution 1
You can either change the position of the urls in the hostName from
hostName="cms.mysite.com|www.mysite.com|33.44.54.123"
to
hostName="www.mysite.com|cms.mysite.com|33.44.54.123"
Then when you split, you take the first one.
Proposed Solution 2
Make use of the targetHostName in your SiteDefinition.config as follows:
hostName="cms.mysite.com|www.mysite.com|33.44.54.123"
targetHostName="www.mysite.com"
Then, make use of the code
var url = Sitecore.Context.Site.TargetHostName;
EDIT 1
It is better to not use Sitecore.Context.Site.HostName but Sitecore.Context.Site.TargetHostName because the HostName property is ok while you one have only one Hostname per site in the web.config.
The TargetHostName take the property TargetHostName of the site if specified in the site tag of the web.config or the first hostname if this property is not specified.
Reference: http://sitecoreblog.blogspot.com/2010/12/dont-use-hostname.html?m=1