ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How best to create short code redirect in EE?

November 18, 2014 1:07pm

Subscribe [2]
  • #1 / Nov 18, 2014 1:07pm

    Eric N

    28 posts

    We currently have all the entries to use the URL title and not the entry ID. However, these get long obviously and can be a pain when tweeting etc.

    I was thinking a short code set up would be simple, but maybe I’m missing the obvious there.

    I’m thinking something like

    <a href="http://www.site.com/short/123445">http://www.site.com/short/123445</a>
    that would look up the channel and URL title and redirect as appropriate to something like
    <a href="http://www.site.com/news/some-super-long-title-for-an-article-or-blog">http://www.site.com/news/some-super-long-title-for-an-article-or-blog</a>

    Anyone able to point me in the right direction? I looked at the redirect tag but that just redirects to a template correct?

    Thanks for any help!

  • #2 / Nov 19, 2014 6:47am

    Giraffentoast

    152 posts

    Yeah, that’s quite possible via embed or redirect. 😊

    Just create a template at /short/index and do something like:

    {exp:channel:entries entry_id="{segment_2}" dynamic="no" disable="categories|category_fields|member_data|pagination"}
    
        {redirect='{channel_short_name}/{url_title}'}
    
    {/exp:channel:entries}
  • #3 / Nov 19, 2014 10:03am

    Eric N

    28 posts

    Perfect thanks! Its exactly how I thought it should of been.. I should of tested first!

    I had to modify a little because my templates don’t all match my channel names and threw the 301 code in there as well.

    I think the last if error catch isn’t needed, but it can’t hurt.

    {exp:channel:entries channel="public|premium" status="open|featured"  entry_id="{segment_2}" dynamic="no" disable="categories|category_fields|member_data|pagination"}
    {if no_results}
      {redirect="site/index" status_code="301"}
    {/if}
     {if channel_short_name=="public"}
      {redirect="news/{url_title}" status_code="301"}
     {if:elseif channel_short_name=="premium"}
      {redirect="premium/{url_title}" status_code="301"}
     {if:else}
      {redirect="site/index" status_code="301"}
     {/if}
    {/exp:channel:entries}
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases