We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

NEW - Redirect Plugin…

Development and Programming

Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hiya,

A really quick post this one and a really simple plugin but it may come in handy for someone somewhere.

Plugin Usage : This plugin is exceptionally simple to use. Place the following tag on any template as shown below : Using full URL - useful for external sites.
{exp:redirect_to location="http://www.yahoo.com"}
or using paths instead - useful for internal links.
{exp:redirect_to location="news/detail/news_story"}
By default PHP redirection will be performed. If PHP redirection does not work, use javascript redirection by adding method=”script” parameter.
{exp:redirect_to location="news/detail/news_story" method="script"}
If it is impossible by usual means to check if condition for redirection was met or not you can check it by wrapping the code expressing condition by {exp:redirect_to}{/exp:redirect_to} tags. Redirection will be performed if the code wrapped by {exp:redirect_to}{/exp:redirect_to} tags will produce no output.
{exp:redirect_to location="my_template_group/my_template" method="script"}
{exp:query sql="SELECT title FROM exp_weblog_titles WHERE weblog_id = '5' AND url_title ='My title'"}
{title}
{/exp:query}
{/exp:redirect_to}
If you want that redirection would be performed on condition that certain output were produced, you should enter expected string as a value of “tagdata” parameter. Then the redirection will be performed if the string entered as “tagdata” parameter value and output of the code wrapped by {exp:redirect_to}{/exp:redirect_to} tags are identical. (Tabs, spaces and new line characters will be stripped.) In example below redirect will occur if {exp:query tag} will produce string “1”.
{exp:redirect_to location="my_template_group/my_template" tagdata="1" method="script"}
{exp:query sql="SELECT title FROM exp_weblog_titles WHERE weblog_id = '5' AND url_title ='My title'"}
{total_results}
{/exp:query}
{/exp:redirect_to}

Basically if you place this into a template then you will be re-directed to the URL specified in the location parameter.

Hope it helps.

Best wishes,

Mark

       
oldgoldblack's avatar
oldgoldblack
113 posts
17 years ago
oldgoldblack's avatar oldgoldblack

Hey Mark, interesting little nugget. I assume EE variables can be used within this parameter? Such as:

{exp:redirect_to location="http://www.mysite.com/new_directory/{weblog}/{url_title}/"}

Or perhaps:

{exp:redirect_to location="http://www.mysite.com/new_directory/{segment_3}/"}

Also, is there a delay on this rederict? And forgive a novice’s question - how does this compare with a meta-equiv?

Thanks!

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hi Kevin,

I must admit I hadn’t even checked to see if those would work or not but I just tried with your second example with the {segment_3} option and that seems to work fine. The top one I haven’t tried yet but not too sure where you are getting those variables from? For instance the {weblog} variable. Is that a Global variable that you have created?

Best wishes,

Mark

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Oops sorry meant to answer the second bit too!!

The documentation for that function in ExpressionEngine says the following :

redirect(location [string]) Redirect to location. Class: Functions (FNS) Description: Redirects current browser page to location. Does redirect either by location or meta refresh, depending on the redirect method preference.

Hope that answers the second question as to tell the truth I wasn’t too sure myself 😉

Best wishes,

Mark

       
tpayton's avatar
tpayton
172 posts
17 years ago
tpayton's avatar tpayton

Mark,

Sweet plugin. However I was trying to us it within an if statement and can’t get it to work properly (it seems to redirect regardless).

Have you had a chance to use it in an if statement?

  • T.
       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hiya,

In what way are you using it in an IF statement? Can you show some code?

Best wishes,

Mark

       
tpayton's avatar
tpayton
172 posts
17 years ago
tpayton's avatar tpayton

Mark here is what I got:

{if segment_3 == "{exp:member:custom_profile_data}{pebble_group}{/exp:member:custom_profile_data}" }
You are on the right page.
{if:else}
You are on the wrong page my friend. Let me redirect you.
{exp:redirect_to location="http://www.mysite.com/group/"}
{/if}

If I leave out your redirect code the text outputs properly. With your code, it redirects regardless of weather the conditional is true or not.

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hmm,

I just tried it out and as you are seeing it doesn’t work but I did try a simple :

{if segment_3 == "test"}
Spit out this text
{/if}

{if segment_3 == "none"}
{exp:redirect_to location="http://www.mysite.com/group/"}}
{/if}

That seems to work fine. Not too sure what is up with the ifelse structure though. Perhaps one of the admins could pop in here and let us know?

Best wishes,

Mark

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Sounds good to me but then I’m not really sure either!! 😉

Best wishes,

Mark

       
tpayton's avatar
tpayton
172 posts
17 years ago
tpayton's avatar tpayton

Thanks guys. I learn something new everyday.

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Thanks for the thanks but they really should go to Victor 😊

Hope the extra if statements work for you though and thanks for the comment on the plugin!! 😊

Best wishes,

Mark

       
Andy Harris's avatar
Andy Harris
958 posts
17 years ago
Andy Harris's avatar Andy Harris

Hey Mark,

This is perfect for my current need. However I think I’ve run up against the advanced conditional problem too:

{if logged in}
    Do this
{/if}

{if logged_out}
   {exp:redirect_to location="/members"} 
{/if}

Is there an easy way I can get around this issue?

Hope someone can help, Andy.

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hi Andy,

Hmm yes it does seem to be a problem with these conditionals. I will have to look into that when I get the chance. For now though could you possibly embed a template that does what you need?

Something like :

{if logged_in}
{embed="do_this_template"}
{/if}

{if logged_out}
{embed="redirect-to-template"}
{/if}

Not sure if that would work but hopefully it will?

Best wishes,

Mark

       
Andy Harris's avatar
Andy Harris
958 posts
17 years ago
Andy Harris's avatar Andy Harris

It’s certainly worth a try, I’ll give it a bash (so to speak) and let you know how it goes.

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hi Andy,

I just gave this a try and it will definitely do what you need. Also just noticed that in your code you had logged in instead of logged_in. Not sure if that will help with this though although that code is incorrect.

Best wishes,

Mark

       
1 2 3 Last

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.