Is there an available variable for the primary site URL in a multi-site CraftCMS setup? I need a variable that I can attach to my logo that will take the user back to the primary site. I could hard code this, however, the URL needs to be environment aware. For example, on my local environment, the link should be https://www.mysite.local and when on development, the link should be https://www.mysite.dev.

up vote 2 down vote accepted

You can output the primary site’s Base URL like this:

{{ craft.app.sites.primarySite.baseUrl }}

(craft.app.sites maps to craft\services\Sites.)

If your Base URL begins with an alias, you will need to parse it:

{{ alias(craft.app.sites.primarySite.baseUrl) }}

Altertatively, if you need to link to a sub-page of the primary site (not just its Base URL), you can do that with the siteUrl() function, which has a siteId argument.

{% set primary = craft.app.sites.primarySite %}
{{ siteUrl('sub/path', siteId=primary.id) }}

Note that if your primary site’s Base URL contains the @web alias, that will always just return the host info for the current request. So you may need to replace that with a hard-coded base URL (or use a custom alias) before this works as expected.

  • This also gives the URL of the site I'm on, not the default site in a multi-site installation. The default site in my multi-site installation is www, and I have 5 subdomains. So, when I'm on blog.mysite.com and I use url(), I get blog.mysite.com -- this is not what I'm looking for. What I really want is www.mysite.com -- the default site in the multi-site installation. – Vaughn D. Taylor yesterday
  • 1
    @VaughnD.Taylor Sorry, misunderstood your question. By “default” I take it you meant the primary site. Updated your question and my answer. – Brandon Kelly yesterday

{{ siteUrl }} should do the trick for you.

  • This gives the URL of the site I'm on, not the default site in a mult-site installation. – Vaughn D. Taylor yesterday

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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