2

Let's say we have a url: http://example.com/section/my-url-title we want to enure that the {url_title} is not used in the URI. We have changed all template tags to point to the {entry_id} instead.

The issue now is we have 600+ url's listed with google. We therefore want a 301 re-direct that checks for a {url_title} and then sends a 301 header with the new URI schema using the {entry_id}

As an example of what we want to achieve, let imagine the URL is http://example.com/section/my-url-title this then re-directs to http://example.com/section/369

We set up as basic template which works as you might expect:

{exp:channel:entries}
   {if '{segment_2}' == '{url_title}'}
      I should re-direct {url_title} | {entry_id} -- <? echo({entry_id}); ?>
   {if:else}
      I should NOT re-direct {url_title} | {entry_id} -- <? echo({entry_id}); ?>
   {/if} 
{/exp:channel:entries}

And as you might expect a request to http://example.com/section/my-url-title outputs this into the template:

I should re-direct my-url-title | 369 -- 369

If we then test the url using the entry ID URI http://example.com/section/369 we get:

I should NOT re-direct my-url-title | 369 -- 369

...so far so good...

We then added the following php within the true part of the if statement:

<?
    Header( "HTTP/1.1 301 Moved Permanently" );
    Header( "Location: /{segment_1}/{entry_id}" );
    die();
?>

And the template re-directs BUT we get caught in a re-direct loop. This is the same for either URLS:

http://example.com/section/my-url-title http://example.com/section/369

Both get in a re-direct loop.

We have also tried this plugin: http://blog.builtbyprime.com/php/301-redirect-for-expression-engine-2 which also seems to get stuck in a loop and disregard the if statement.

Any help or advice greatly appreciated.

p.s I'd rather avoid the task of remapping every url using googles webmaster tools.

1
  • Does the redirect loop always go to the first?
    – GDmac
    Mar 4 '13 at 14:35
2

You can't use {if}...{if:else}...{/if} (always executed, output thrown away after conditional check) but have to use switchee or something similiar.

{exp:switchee variable="{segment_2}" parse="inward"}
    {case value="{url_title}"}
        I should re-direct {url_title} | {entry_id} -- <? echo({entry_id}); ?>
    {/case}
    {case default="Yes"}
        I should NOT re-direct {url_title} | {entry_id} -- <? echo({entry_id}); ?>
    {/case}
{/exp:switchee}

switchee docs:

With EEs if/else conditionals, each condition is parsed before being removed at the end of the parsing process. This means if you wrap if/else tags around lots of other tags then your template will be running many unnecessary queries and functions.

As Switchee is a tag we can use parse=“inward” to ensure that unmatched conditions are not parsed before being removed from the template.

2
  • Ah yes, perhaps the other option to place the redirect in an embedded template? Mar 4 '13 at 15:31
  • Looks like a simple embed is the way to go! Thank you everyone, one I have a proof of concept working I'll post the results here.
    – paulcripps
    Mar 5 '13 at 10:01
0

A simple embed did the trick, we simply check {if '{segment_2}' == '{url_title}'} and it its true I have an embed that then uses this plugin to redirect: http://blog.builtbyprime.com/php/301-redirect-for-expression-engine-2

Job done :)

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.