Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2018-12-13 10:48:30

jakob
Moderator
From: Germany
Registered: 2005-01-20
Posts: 3,344
Website

Make a section appear in a subdomain?

I’m looking for a way to place a specific section of an existing textpattern installation in a subdomain without having to setup a completely new textpattern installation just for those pages. Is that feasible, perhaps with the help of ied_if_domain or another tag (e.g. might txp:page_url work)?

My web host allows me to point a subdomain to the same folder as the main domain containing the textpattern installation.

I have two use cases:

  1. A directory of suppliers with corresponding downloads of their official product certificates that needs to have a permanent location for years to come.
  2. A site for a summer school which needs an archive of the blog entries for each year, e.g. 2017.domain.com, 2018.domain.com along with a brief introductory text about that year’s summer school.

Presumably, I’d need to do:

<txp:ied_if_domain domain="section.domain.com">
    <!-- template code for specific section -->
<txp:else />
    <!-- regular page template code -->
</txp:ied_if_domain>

In connection with that, is it possible to redirect straight to a different page template, like txp:output_form but with a page template, or is it more straightforward to create a dedicated form that more or less replicates what the page template does and output that?

Are there any other problems I might encounter with this kind of setup?


TXP Builders – finely-crafted code, design and txp

Offline

#2 2018-12-13 13:54:46

Destry
Moderator
From: Haut-Rhin
Registered: 2004-08-04
Posts: 4,144
Website

Re: Make a section appear in a subdomain?

Watching this one closely, because it seems a trend these days is to use subdomains as additional websites (I’m doing that now for my own projects) instead of always buying more and more costly domains. I don’t mind a different install if the site is a bit more complex, but if it’s something that only needs one section and can benefit from a distinctive URL, all the better.

Lots of use cases. A few examples for just a personal site alone:

  • resume.domain.tld (a personalized work thing), or…
    • profile.domain.tld (à la ‘about.me’, but actually owned by you)
    • bio.domain.tld (ditto)
  • social.domain.tld (e.g. a place to stream a social micro-post account), example…
    • masto.domain.tld
  • blogroll.domain.tld (unsightly side columns aren’t my thing anymore)
  • library.domain.tld, or…
    • biblio.domain.tld (i.e. a curated bibliography on some subject of interest)
  • manifesto.domain.tld (or any other decree from high podium or silver horse)
  • etc

Maybe I’m getting carried away on some of these, but I actually like these URL structures much better than this:

  • domain.tld/resume/
  • domain.tld/biblio/
  • etc

Especially as I like using title-only URLs now, though it wouldn’t matter in this case, I guess.

I’m sure there are business case uses too.


The text persuades, the *notes prove。

Offline

#3 2018-12-13 14:36:51

etc
Developer
Registered: 2010-11-11
Posts: 3,106
Website

Re: Make a section appear in a subdomain?

jakob wrote #315563:

is it possible to redirect straight to a different page template, like txp:output_form but with a page template, or is it more straightforward to create a dedicated form that more or less replicates what the page template does and output that?

Are there any other problems I might encounter with this kind of setup?

Both are possible, just keep in mind that if you create a HTML type form to be used as page template, it will be available to any article via f=mypage URL query. The second approach leaves you with more control since only the articles of the concerned sections will be able to use their page template. There is no “official” way to use another page than this assigned to the section afaik.


etc_[ query | search | pagination | date | tree | cache ]

Offline

#4 2018-12-13 17:25:59

jakob
Moderator
From: Germany
Registered: 2005-01-20
Posts: 3,344
Website

Re: Make a section appear in a subdomain?

Destry wrote #315567:

Lots of use cases. A few examples for just a personal site alone:

  • resume.domain.tld : profile.domain.tld / bio.domain.tld
  • social.domain.tld : masto.domain.tld
  • blogroll.domain.tld
  • library.domain.tld / biblio.domain.tld
  • manifesto.domain.tld
  • etc

… but I actually like these URL structures much better than this:

  • domain.tld/resume/
  • domain.tld/biblio/
  • etc

I guess there’s nothing stopping you doing that but I’m not sure whether subdomains are such a great idea as a replacement for typical sections. In terms of SEO I guess that they are more or less equal: the keywords are there in the url with both variants. I’ve no idea whether one is rated better than the other. Another thing to watch out for is if you don’t have a wildcard certificate, you’ll need an SSL certificate for each subdomain (or extend your SSL certificate to include each new subdomain).

I do like subdomains as a way of segmenting or containing distinct data, e.g. your private site and your business site as you have done.

In my examples:

  1. the first example is really intended as a fixed URL that I can publish in a print document now and will be still be valid in 5 years time. We have general info pages on the certificates (environmental product declarations) on the regular site with a sample, but suppliers will need to produce these EPDs for their own products. At present, there are very few and I suspect a simple list will suffice (hence just making a section of the main site) but maybe in a few years time there will be a long list of products and I may need to provide a better search facility, or maybe a means by which the suppliers can manage their data themselves. At that point I could do switch to a dedicated setup for that subdomain.
  2. The second example is for instances of a concept, in this case “editions” of a summer school. I’m in two minds here. At present I’m converting an old weebly-based site to show the results from past summer schools so the section as a subdomain variant appeals to me. In future, the participants may need login access to the site, in which case it might be better to make separate multisites so that there’s no risk of external user logins being able to see/edit/delete material on the main site or on the other year’s sites because they’re totally separate instances.
etc wrote #315568:

Both are possible, just keep in mind that if you create a HTML type form to be used as page template, it will be available to any article via f=mypage URL query.

I didn’t know that. Is that something that can be done with any section on any site? If so, that looks like a way of discovering information not intended for consumption. Maybe that needs locking down…

The second approach leaves you with more control since only the articles of the concerned sections will be able to use their page template. There is no “official” way to use another page than this assigned to the section afaik.

The second approach being to use txp:output_form form="my-section-page-as-a-form"?

since only the articles of the concerned sections will be able to use their page template.

Am I not going to have that problem either way, as I’m going to have to apply ied_if_domain to the default template anyway for the subdomain? You can use article_custom to show articles from another section and kind-of replicate the behaviour of txp:article, but it’s a bit laborious.

Might there be a way of doing something like this:

<txp:ied_if_domain domain="section.domain.com">
    <!-- set section context to desired "section" -->
    <!-- if using year subdomains, set category/date context to match "subdomain" -->
    <!-- call form/page_template -->
<txp:else />
    <!-- regular page template code -->
</txp:ied_if_domain>

Or can I only achieve that by making a plugin to do some special routing prior to the page template being called?


TXP Builders – finely-crafted code, design and txp

Offline

#5 2018-12-13 21:46:37

etc
Developer
Registered: 2010-11-11
Posts: 3,106
Website

Re: Make a section appear in a subdomain?

jakob wrote #315573:

I didn’t know that. Is that something that can be done with any section on any site? If so, that looks like a way of discovering information not intended for consumption. Maybe that needs locking down…

It’s 4.7.2 custom assets in action :-) Yes, they should be used with caution, keeping in mind their public accessibility. But only custom media type forms can be accessed this way, and they are set in advanced prefs pane, so this presumes some knowledge anyway. That’s like rah_external_output works too, btw.

Might there be a way of doing something like this:

<txp:ied_if_domain domain="section.domain.com">...

Or can I only achieve that by making a plugin to do some special routing prior to the page template being called?

If .htaccess rewrite is not an option, then yes, you’d need a plugin called on pretext for this, I’m afraid. It should populate $_GET with s field you extract from URL. No warranty…


etc_[ query | search | pagination | date | tree | cache ]

Offline

#6 2018-12-14 10:06:53

Destry
Moderator
From: Haut-Rhin
Registered: 2004-08-04
Posts: 4,144
Website

Re: Make a section appear in a subdomain?

jakob wrote #315573:

I guess there’s nothing stopping you doing that but I’m not sure whether subdomains are such a great idea as a replacement for typical sections.

Yeah, you’re probably right. It’s certainly not easier, which is enough reason to not bother in my examples.


The text persuades, the *notes prove。

Offline

#7 2018-12-14 15:00:13

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 7,107
Website

Re: Make a section appear in a subdomain?

I guess it has to do with url semantics. I wish we had a non plugin option for /section/category/title as it would alleviate the need of many sub-domains.


Yiannis
——————————
neme.org | hblack.net | LABS | State Machines | NeMe @ github

Offline

#8 2018-12-21 23:11:02

jakob
Moderator
From: Germany
Registered: 2005-01-20
Posts: 3,344
Website

Re: Make a section appear in a subdomain?

Just to recap, I’ve tried this out now for my second example and have it more or less working, but would welcome any improvements. The description may look long, but it’s actually quite simple to implement with very little code.

The principle: make one section of a single Textpattern installation accessible via some subdomains. Here the intention is to create annual archives of a summer school blog along with a brief overview of that year using articles in the blog section. The same principle could also be applied to conferences, editions of a magazine or similar.

Step 1 – Point your domain and subdomains to the same path with your web host:

www.mysite.com   ->  /path/to/my/site/
2015.mysite.com  ->  /path/to/my/site/
2016.mysite.com  ->  /path/to/my/site/
…

The site_url of the site is set to www.mysite.com. If using https, you’ll need to create SSL certificates for each subdomain (or have a wildcard certificate for the domain and subdomains).

Step 2 – Create categories for each year’s event following a regular pattern, e.g. summerschool-2015, summerschool-2016 and so on. If you have any files or image galleries that belong to that event, create similar file or image categories.

Step 3 – Assign the blog articles to the respective categories. For an introductory article, I chose to write a sticky article also in the blog section, likewise assigned to the category. Again, if you need to provide file downloads or an image gallery, assign your files/images to the respective year’s categories.

Step 4 – Page template and forms

For the sake of simplicity, I have three main page templates:

home.txp  –>  for the homepage + subdomain homepages
blog.txp  –>  for the blog pages
main.txp  –>  the other summer course information pages

The go-to-plugins for domain detection ied_if_domain and aam_if_host are very similar and as Ruud pointed out are essentially just a wrapper for a couple of lines of php, so I just put the php in a form and simplified it down to the subdomain name as the domain remains the same:

Form context of type ‘misc’:

<txp:hide>
   +++ Get subdomain from server name (single-level subdomains only) +++
</txp:hide>

<txp:variable name="subdomain"><txp:php>
    $site_host = $_SERVER['SERVER_NAME'];
    echo explode('.', $site_host)[0];
</txp:php></txp:variable>

Output this form with <txp:output_form form="context" /> at or near the top of your page templates, or in a form such as html_head that you call from the beginning of every template.

You can now use the following in your page templates to determine what to show depending on the subdomain specified, e.g. for the homepage home.txp:

<txp:if_variable name="subdomain" value="www">
    <!-- main site: regular homepage code -->
<txp:else />
    <!-- other subdomain: landing page code -->
</txp:if_variable>

and for the blog page template blog.txp:

<txp:if_variable name="subdomain" value="www">
    <!-- main site: link list to the available subdomains -->
<txp:else />
    <!-- subdomain: blog list / display code -->
</txp:if_variable>

One more problem remains: the other pages of the main site should not be viewable on the annual subdomains, only from the main www subdomain. To prevent that, create the following form:

Form subdomain_check of type ‘misc’:

<txp:if_variable name="subdomain" value="www" not>
    <txp:variable name="redirect_url"><txp:site_url /><txp:page_url /></txp:variable>
    <txp:php>
        global $variable;
        header('Location: '.str_replace("//", "/", $variable['redirect_url']));
        exit;
    </txp:php>
</txp:if_variable>

and include it as <txp:output_form form="subdomain_check" /> somewhere near the top of the page templates that should not show on the yearly subdomains: main.txp in the above example. The form should be included after the context form.

If you design your internal page links carefully, this code may never be used, but it catches any forgotten cases or people who try out the url.

Step 5 – Filter output to match the corresponding year’s subdomain

Having the subdomain saved as a variable also makes it possible to show only articles from 2015 on the subdomain 2015.mysite.com by using the subdomain variable to filter the output by category name.

The subdomain landing page code has the following pattern:

<!-- Intro article (sticky blog article) -->
<txp:article_custom category='summerschool-<txp:variable name="subdomain" />' limit="1" status="sticky">
    <txp:title wraptag="h2" />
    <txp:body />
</txp:article_custom>

<!-- Blog overview -->
<h3>Blog articles</h3>
<txp:article_custom category='summerschool-<txp:variable name="subdomain" />' limit="99" status="live" sort="Posted asc" break="" wraptag="ul" form="article_blog_listing" />

and the subdomain blog list / individual blog article is like this:

<txp:php>
    // Force category context to match subdomain prefix
    global $variable, $pretext;
    $pretext['c'] = 'summerschool-'.$variable['subdomain'];
</txp:php>

<txp:article form="article_blog_single" listform="article_blog_listing" sort="Posted asc" break="" limit="99" />

For each new year (conference, issue, edition, …) you create a new subdomain pointing to the same installation path, create a new category (where required also image/file categories) and write/upload content and assign it to that category.

Step 6 – Make a chooser/menu linking to the subdomains

You could generate your list of links to the subdomains from the corresponding categories by putting all your categories into a parent category and using txp:category_list with the parent attribute.

I opted instead to create a variable for the site that contains the available subdomains, e.g.

<txp:variable name="archive_years">2015,2016,2017</txp:variable>

Again, this needs to appear very high up on your page template (e.g. add it to the context form). Or: using oui_prefs or adi_variables you can make this variable editable by the site admin.

To use that variable to create a link list, you can use rah_replace (smd_each would work too) as follows:

<txp:rah_repeat value='<txp:variable name="archive_years" />' wraptag="ul" break="li">
    <a href="<txp:smd_wrap transform='replace||www|<txp:rah_repeat_value />'><txp:site_url /></txp:smd_wrap>"><txp:category name='summerschool-<txp:rah_repeat_value />' title="1" /></a>
</txp:rah_repeat>

I’ve used smd_wrap to replace www with the corresponding subdomain year, but you could do that with php, or alternatively create a second domain variable in the context form right at the beginning and then chain together https://<txp:rah_repeat_value /><txp:variable name="domain" /> to construct the links.

Remaining odds and ends

There are still some small bits and pieces you need to remember to cater for in your code:

  • Links within a subdomain must be manually constructed to use the 2015 (etc.) subdomain rather than using txp:site_url or txp:permlink.
  • Links to pages on the main site that should not show on the subdomain, such as the regular course info pages, must be constructed using txp:site_url, etc.
  • Similar to the above, canonical urls, links in feeds and so on need to handled specially depending on the subdomain.

TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB