4

We have set up two CD server( http://cds1mysite.com & http://cds2mysite.com ) in clustered environment where redirection to these URLs handled by LoadBalancer. Website public URL is https://www.mysite.com So If we use following code to get server URL it's returning CD server URL not public URL. Instead it should return https://www.mysite.com

string serverurl = Sitecore.Globals.ServerUrl;

Do we have any other way to access public URL

1 Answer 1

6

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

7
  • actually in sitedefinitation.config file we have only following hostname attribute not targethostname hostName="cms.mysite.com|cmsmysite.us|33.44.54.123" so I won't get if I use Sitecore.Context.Site.HostName well why we need targethostname if we have hostname attribute
    – Sukhjeevan
    Commented Jan 26, 2017 at 3:06
  • This attribute targethostname will be used by the SiteResolver if it is not blank. It will use the HostName. By default targetHostName is blank. So you can use Sitecore.Context.Site.HostName and since you have delimited it by pipe, you will need to check into the SiteResolver Commented Jan 26, 2017 at 3:12
  • as you mentioned targethostname will be used by SiteResolver but as I don't have targethostname attribute so I won't get any thing. Am I correct here? Other option I'm thinking about put the mysite.com text in Settings.config file and directly access through in code by settings code.How is it?
    – Sukhjeevan
    Commented Jan 26, 2017 at 3:40
  • Why would you not able to retrieve the hostname? Since you have specified the HostName, you can use the Sitecore.Context.Site.HostName Commented Jan 26, 2017 at 3:43
  • first of all I just want to correct hostname and it's hostName="cms.mysite.com|www.mysite.com|33.44.54.123" so If I use Sitecore.Context.Site.HostName I got "cms.mysite.com|www.mysite.com|33.44.54.123" Did you mean that I should get this string seperated by pipe and where I get www fetch that string from array. Is it?
    – Sukhjeevan
    Commented Jan 26, 2017 at 4:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.