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.or using paths instead - useful for internal links.{exp:redirect_to location="http://www.yahoo.com"}
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"}
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="news/detail/news_story" method="script"}
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" 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}
{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
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!
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
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
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.
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
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.
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
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.