0

right now I have an URL-structure like

www.xy.com/channel/template/url-title

Is it possible to get an url-structure like

www.xy.com/url-title

instead, removing all segments except the url-title? Maybe with the help of template routes, but I am not sure how to use it in this case.

Thanks for any hints!!

1

Whether you can depends somewhat on how your site / templates are designed.

As far as EE is concerned, you don't really need to specify which channel or anything to retrieve an entry - the url_title is enough on its own. So if you want to use the url structure domain.com/url_title there is no reason why you could not do use a template design like this:

{exp:channel:entries url_title="{segment_1}" ... other parameters ... }
    <h1>The title of this entry is {title}.</h1>
{/exp:channel:entries}

By itself the weakness with this approach is that it sort of pushes you to having one template to drive the whole site... which undermines some of the strength of EE as a CMS: having other segments in the URL is useful to help direct EE towards which template group to use, and to drive some features and responses arising from the user's activities on the site. But if having just one segment is important to you, you can maybe work around this by building in conditional logic to your master template... there are two things that might be worth considering:

  • Once you have retrieved a channel entry using the url_title you can then access other information about the entry - such as what channel it is in, its ID, what categories have been assigned to it etc;
  • The EE tempate layout system is quite flexible and allows you to build a template step-by-step based on information retrieved in earlier templates.

So you could have a 'parent' template that simply runs a channel entry call with the url_title, and then has a bunch of conditional logic to work out what template to load based on what information you retrieve from that call.

To do the redirect to the correct template without messing with the URL, you need one more trick. EE (at least up to EE5) does not allow for conditional terms in template layout tags, but does allow you to use layout variables - so use this switching approach you need to do a two-step thing:

  1. Set the parent template to transfer processing to a fixed template (do this by putting {layout='some_template_group/some_template} as the first line of your parent template.
  2. In your parent template use your conditional logic to identify the correct template to use (and any other important information derived from this knowledge) and use this to set the value for a layout variable (call it something like processing_template) - so something like
{layout:set name="processing_template"}template_group/template{/layout:set}
  1. Then put this entry as your first line of the 'processing_template`
{layout='{layout:processing_template}'}

Then EE when it processes one of your single term URLs will not only process the parent template, but also append the content of whatever other template you want based on the conditional logic processing...

Hope this helps.

| improve this answer | |
  • 1
    Thanks for this indepth explanation! As I have a pretty extensive template-structure and a bigger site I have to consider how many efforts are in this, but most probably this is exact the answer I was looking for! – BNetz Oct 19 at 16:56

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.