In Craft, there is no such thing as a "section path" – there's only the "Entry Template" path (which tells Craft which template to load when the URL for an Entry in that particular section is requested) and the "URL format", which tells Craft how it should construct the URLs for the entries within that section.
It's actually totally possible to have multiple sections use the same URL format – e.g. blog/{slug}
– so that part is easy; just use the same format for both of your blog sections, and all your URLs will be consistent out of the box (the only drawback to be aware of is that if you create entries with identical names within both sections, Craft will suffix the auto-generated slug for the newer entry with a number, i.e. my-generic-blog-post-title-2
).
I'm assuming your blogs should use different templates for both their index and entry pages, so with the above URL format in place, it's simply a matter of routing the requests to the correct template(s).
The entry template is easy. You can set different paths for the two blog section's Entry Template path settings, e.g. blog/blogA/_entry
and blog/blogB/_entry
, or you can use the same path, e.g. blog/_entry
for both. If you use the same template, you can conditionally {% include %}
a blog specific template by testing the entry.section.handle
value, e.g.
{% switch entry.section.handle %}
{% case 'blogA' %}
{% include 'blog/blogA/_entry' %}
{% case 'blogB' %}
{% include 'blog/blogB/_entry' %}
{% endswitch %}
For the blog indexes – i.e. domain.com/blog
and domain-two.com/blog
– it's important to realize that in Craft, there is no inherent relationship between templates and sections. For instance, even if you have a section with the handle blog
and a template templates/blog/index
, Craft won't automatically "connect" the template and the section, in terms of, for instance, populating the template with entries or other data related to the blog
section.
What Craft will do, is to route all templates (that aren't hidden using an underscore prefix, i.e. _secret.html
) to URLs. This means that if you do have a template under templates/blog/index.html
or templates/blog.html
, Craft will load that template whenever the URL domain.com/blog
(and domain-two.com/blog
) is requested.
In your case, you can exploit this behaviour by creating a template called templates/blog/index.html
. Inside that template, you can add a {% switch %}
statement similar to the one in the entry template, but this time looking at the craft.request.serverName
variable (I'm assuming you've created index templates for each of your blog sections under templates/blog/(sectionHandle)/index.html
):
{% switch craft.request.servername %}
{% case 'domain.com' %}
{% include 'blog/blogA/index' %}
{% case 'domain-two.com' %}
{% include 'blog/blogB/index' %}
{% endswitch %}
If you're looking for a more robust solution, you should look at Craft's multi-environment features, or possibly make use of the appId
config setting; i.e. use different appId
values for the two domains, and load templates conditionally based on craft.config.appId
instead of hard coding the hostnames in your templates. To have Craft set a different appId
based on the different domain names, you could add something like this to your craft/config/general.php
file:
return array(
'domain.com' => array(
'appId' => 'siteA',
),
'domain-two.com' => array(
'appId' => 'siteB',
),
);