1

I have a Craft installation with two domains pointing at it. The first domain is all working and has a number of sections including a blog located at /blog. What I want to be able to do is have a different blog section for the second domain that is also at the path /blog so naming conventions are all consistent i.e.

domain.com/blog
domain-two.com/blog

Of course in the CP when you add another section it will not let you use the same path which makes sense. As there is a lot of shared information but also a couple of sections/pages that will be different I thought perhaps using the .htaccess to rewrite the URL to include an additional path segment for the second site which could then be used in the CP to distinguish unique paths.

I tried modifying the .htaccess RewriteRule but could not get this to reflect in the information passed through to Craft.

RewriteCond %{HTTP_HOST} ^domain-two.com$
RewriteRule (.+) domain-two/index.php?p=$1 [QSA,L]

or

RewriteCond %{HTTP_HOST} ^domain-two.com$
RewriteRule (.+) index.php?p=domain-two/$1 [QSA,L]

Is there a way to make the rewrite work, I'm probably doing something wrong, or is there a better way to trigger a unique path from the same URL path? I really need to keep the URLs consistent.

2 Answers 2

2

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',
    ),
);
0

So I was making this more complicated than necessary, there was no need to go near the .htaccess. In the end I used something similar to Mats' answer and used a dynamic include. Each site has templates in a sub-folder and the prefix for this is set in the general.php config settings based on the URL. It's then used in the following include statement in the relevant template.

{% include '_' ~ craft.config.sitePrefix ~ '/path/to/template' %}

With regard to my poor explanation, by 'path segments'I actually meant the 'Entry URL Format' which must be unique for singles which was where I was confused. For the blog channels for each site I could set it to blog/{slug} and let Craft sort it out displaying the correct template set in the settings. Any duplication in singles' URLs was overcome with a generic include template that switched based on the config setting.

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.