Redirect when last segment missing
Asked Answered
C

3

6

I have a single entry url structure like:

www.site.com/template_group/template_1/entry_id

But I want it to be:

www.site.com/template_group/template_1/entry_id/url_title.

Entry_id would be the segment feeding exp:channel:entries. How can I redirect www.site.com/template_group/template_1/entry_id to www.site.com/template_group/template_1/entry_id/url_title.

Thanks a lot!

Crusted answered 6/11, 2012 at 9:14 Comment(0)
C
12

Something like the following should work - it's untested, but it'll give you a good idea of how to go forward with it.

{if segment_4==""}
{exp:channel:entries channel="x" limit="1" dynamic="no" entry_id="{segment_3}"}
    {redirect="template_group/template_1/{entry_id}/{url_title}"}
{/exp:channel:entries}
{/if}
Counterstamp answered 6/11, 2012 at 9:30 Comment(0)
H
5

put this at the very top of your template:

{if segment_4 == ""}
   {exp:channel:entries channel="channel_goes_here" entry_id="{segment_3}"}
       {redirect="{site_url}/{segment_1}/{segment_2}/{segment_3}/{url_title}
   {/exp:channel:entries}
{/if}

This will check if you have set a fourth segment, if not it takes the third segment containing your entry_id, feeds it to the channel entries tag, which returns the url_title you need. With this url_title you can easily redirect to the correct page.

Hieratic answered 6/11, 2012 at 9:30 Comment(2)
follow-up: you don't even have to feed the entry_id to the channel entries tag, expressionEngine will take care of that. But specifying the entry_id might result in a faster page-load.Hieratic
bonus point for making seg1 to seg3 dynamic. Get extra bonuspoints for making the to-check segment dynamic/configurable too, with a preload_replace.Device
W
4

Good answers from janvl and madebyhippo

Quick note if you are using complex conditionals (the ones in the answers are simple ones so you will not have that problem) is that EE will parse the channel entries tags they contain, wich can slow down performance.

If you find yourself in that situation, best to avoid the problem altogether using Mark "The croxton" addons like switchee or ifelse

Weight answered 6/11, 2012 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.