Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect when last segment missing

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!

like image 498
user1070143 Avatar asked Nov 06 '12 09:11

user1070143


2 Answers

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}
like image 163
madebyhippo Avatar answered Oct 13 '22 21:10

madebyhippo


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.

like image 33
janvl Avatar answered Oct 13 '22 20:10

janvl