3

We are looking for a way to route a subdomain to a specific template to use as a landing page.

Is there a way to do this without routing through index.html and redirecting to the specified template folder and file?

I can confirm that using the redirect does lose some of the user information, tracking codes etc. However it does retain the referral url from the 3rd party site.

2 Answers 2

9

For this example we have two domains:

http://www.example.com
http://subdomain.example.com

To use different templates for the subdomain, we can accomplished this with these steps:

1) Setup subdomain to point to a different directory on your server:

Domain: /home/wherever/public_html
Sub-domain: /home/wherever/public_html_subdomain

2) Copy your index.php file over to your new subdomain directory and update the CRAFT_TEMPLATES_PATH variable to point to the location you want to have your templates. In this example, both template folders are above web root.

// index.php for domain
define('CRAFT_TEMPLATES_PATH', realpath(dirname(__FILE__) . "/../templates").'/');

// index.php for subdomain
define('CRAFT_TEMPLATES_PATH', realpath(dirname(__FILE__) . "/../templates_subdomain").'/');

Your template folders now look like:

/home/wherever/public_html
/home/wherever/public_html_subdomain
/home/wherever/templates
/home/wherever/templates_subdomain
2

I know you asked for a way to do this without using a redirect, but if it's handled server-side via twig, it would be transparent and virtually instantaneous:

{% if siteUrl == "http://subdomain.site.com/"  %}
    {% redirect 'subdomainFolder' %}
{% endif %}

You could also redefine the default 'index' template using indexTemplateFilenames in your config file's multi-environment return array for that subdomain, but that would affect all index files for that subdomain in every uri and be pretty extreme:

/**
 * The template filenames Craft will look for within a directory to represent the directory’s “index” template when
 * matching a template path to a file on the front end.
 */
'indexTemplateFilenames' => array('index'),

As far as I know, I don't believe you can specify a full url in custom routes, only uri patterns — but perhaps someone else with more experience with this can add-in. And there may be some other options as well, that I don't know about.

2
  • Thanks Douglas - This is totally valid but I'd like to do it without a redirect if possible to retain tracking information , origin source etc. I'm not sure how much of this would remain if we redirect from the homepage. Functionally this works perfectly, we just have an odd use case. Commented Nov 13, 2014 at 21:28
  • Understood... but my understanding is that all requests are getting routed through craft anyway via a similar method (i.e. every request that craft manages is essentially a 404 redirect). I'm not sure there is much of a difference, whether it's initiated by twig in a template or via a craft service method — it's all a server side redirect. This shouldn't affect tracking information or origin source at all. I'm curious to see if anyone know any other options though. Commented Nov 13, 2014 at 21:38

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.