I have a group website which has a main site and 3 sub sites all within 1 Craft Installation. Each sub site has its own Structure Section for Content.

I am adding a Group Wide Search to the main site and sub sites. Sub sites are accessed by subdomain of main site - e.g. property.domain.com agents.domain.com

On a sitewide search if I get results from the subsites the entry.url will just contain the domain from the site it is being searched for.

So for my search results - I will check the handle of the entry to see if it is from the subsites - and then if possible replace the domain.

I can get the handle with:

{% set handle = entry.section.handle %}

Is there a handy way to replace the domain?


You could use the Craft/Twig |replace filter, which supports regex replacements.

Something like:

{% set handle = entry.section.handle %}

{% if handle == 'subSiteSectionHandle %}
    {% set newUrl = entry.url|replace('/regexMatchPattern/', 'replacedWith') %}
{% endif %}
  • great - works 100% - using {% set newUrl = entry.url|replace(craft.request.serverName, 'newhostname') %}
    – mmc501
    Jan 10 '17 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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