Hi Mark,
In ExpressionEngine it is usual to provide only url segments as tag parameter, as here
{path=site/contacts}
In contrast, using Redirect To plugin full url must be provided as “location” parameter.
It seems to me that it would be handy to allow both full url (for redirection to pages located in external sites) and url segments (for redirection to pages located within site).
I think it would be nice if this functionality would be incorporated into new version of Redirect To plugin.
To achieve this is really easy. The PHP function which is wrapped in Redirect To plugin should check if full url is provided and if it is, redirect to location, if not, create url from segments and then redirect.
Here you can see how modified function might look like
function Redirect_to() {
global $TMPL, $FNS;
// Fetch our parameters from the plugin tag
$location = str_replace("/", "/" ,$TMPL->fetch_param('location'));
//check if full url is provided as location parameter
if (strpos($location, 'http') === 0 OR strpos($location, 'http') > 0)
{
//if full url is provided, redirect
$FNS->redirect($location);
}
else
{
//if only segments of url are provided, create full url and redirect
$location = $FNS->create_url($location);
$FNS->redirect($location);
}
}
Hi Mark, In ExpressionEngine it is usual to provide only url segments as tag parameter, as hereIn contrast, using Redirect To plugin full url must be provided as “location” parameter. It seems to me that it would be handy to allow both full url (for redirection to pages located in external sites) and url segments (for redirection to pages located within site). I think it would be nice if this functionality would be incorporated into new version of Redirect To plugin. To achieve this is really easy. The PHP function which is wrapped in Redirect To plugin should check if full url is provided and if it is, redirect to location, if not, create url from segments and then redirect. Here you can see how modified function might look like{path=site/contacts}
function Redirect_to() { global $TMPL, $FNS; // Fetch our parameters from the plugin tag $location = str_replace("/", "/" ,$TMPL->fetch_param('location')); //check if full url is provided as location parameter if (strpos($location, 'http') === 0 OR strpos($location, 'http') > 0) { //if full url is provided, redirect $FNS->redirect($location); } else { //if only segments of url are provided, create full url and redirect $location = $FNS->create_url($location); $FNS->redirect($location); } }
There’s always someone who wants to spoil my fun 😉
Only kidding. I totally hear what you are saying there and I should have done that at the beginning but wasn’t really too sure of how to use all the functions available to me at the time and still messing around with them nowadays!!
Will give the code you have provided a real good looking over and add it in as soon as I can. Thanks for the help on that one.
Best wishes,
Mark
Just to let everyone know that the plugin has now been updated to V1.1 thanks to the excellent pointer by Laisvunas above. Thanks for that mate! 😉 😊 😉
I have placed credit in the plugin to yourself too. If you want a web-address or anything else in there instead then just let me know and I will add that in as soon as possible.
New plugin version can be found in the first post on the first page of this forum thread.
Best wishes,
Mark
Hi Mark,
I see a problem with Redirect To plugin v.1.1:
There are situations when you can test condition, but cannot perform or cannot perform correctly PHP redirection on condition tested.
One example is advanced conditionals as here:
{if segment_3 == "some_string" }
Some code
{if:else}
{exp:redirect_to location="my_template_group/my_template"}
{/if}
This code will redirect regardless condition is true or not.
Another example is conditionals within ExpressionEngine tags as here:
{exp:weblog:entries weblog="my_weblog" category="5"}
{if total_results == 1}
{exp:redirect_to location="my_template_group/my_template"}
{/if}
{/exp:weblog:entries}
or as here:
{exp:weblog:entries weblog="my_weblog" url_title="my_title"}
{if certain_field == ""}
{exp:redirect_to location="my_template_group/my_template"}
{/if}
{/exp:weblog:entries}
These two pieces of code will produce PHP error.
In this thread some people reported that because of the problems with PHP redirects they were forced to switch to Javascript redirects. It seems to me that it is best to use PHP redirects wherever possible and only if it is not possible to use javascript redirects.
With minimal changes Redirect To plugin can perform both PHP and javascript redirects. To enable javascript redirects a new parameter - “method” should be added. Then if “method” parameter is set to “script”, a javascript redirect will be performed, if “method” parameter is not set, then PHP redirect will be performed.
Having possibility to perform both PHP and javascript redirects you can easily everywhere perform redirects using Redirect To plugin: in cases PHP redirect does not work or you are not sure will it work or not, then you can simply set “method” parameter to “script” and you are done.
Here you can see how modified Redirect_to function might look like
function Redirect_to() {
global $TMPL, $FNS;
// Fetch our parameters from the plugin tag
$location = str_replace("/", "/" ,$TMPL->fetch_param('location'));
$method = $TMPL->fetch_param('method');
// Perform a check to see if a full url is not given in the location parameter
if (strpos($location, 'http') !== 0 AND !(strpos($location, 'http') > 0))
{
// If we only find segments then use the create_url function to
// create our URL needed for correct re-direction
$location = $FNS->create_url($location);
}
// Perform a check to see if method parameter supplied
if ($method === FALSE)
{
// If we do not find method parameter then perform PHP redirect
$FNS->redirect($location);
}
else
{
// If we find method parameter then create redirection javascript and output it
$output = '<script type="text/javascript">location.href="'.$location.'"</script>';
$this->return_data = $output;
}
}
So, with modified Redirect_to function to perform PHP redirect we would need this code:
{exp:redirect_to location="my_template_group/my_template"}
and to perform javascript redirect we would need this code:
{exp:redirect_to location="my_template_group/my_template" method="script"}
Hi Mark,
There is yet another problem with redirects: in some situations you not only cannot perform redirect, but you even cannot test if condition on which you would like to perform redirect was met or not.
For example it seems that it is not possible to perform redirect in cases when {exp:query} tag does not output anything. That is, the code
{exp:query sql="SELECT title FROM exp_weblog_titles WHERE weblog_id = '5' AND url_title ='My title'"}
{if total_results == 0}
{exp:redirect_to location="my_template_group/my_template" method="script"}
{/if}
{/exp:query}
will not work.
Neither will the code as this (see thread)
{exp:subsegment segment="3" separator="-" index="0" parse="inward"}
{exp:weblog:entries weblog="my_weblog" url_title="{subsegment}"}
{if no_results}
{redirect="my_template_group/my_template" method="script"}
{/if}
{/exp:weblog:entries}
{/exp:subsegment}
In both examples it is impossible to perform redirect in case the tag outputs nothing since it is impossible to test if the tag has outputted nothing.
I suppose that it may exist also such situation when the tag has some output, but it is not possible before redirecting to test if it has output or if it has expected output.
I gave some thought on this problem and here is a solution:
In order to test if some complicated condition on which redirect should be performed was met, we need to wrap the code expressing that condition with {exp:redirect_to} and {/exp:redirect_to} tag pair and to check if the tagdata of this tag pair is equal or not equal to some expected string or not.
Here is the code using which you would be able test if {exp:qeery} tag has no output and redirect if it has 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 parameter “tagdata” would be added then there would be possible to test not only if there is output and redirect if there is no output, but also to test if there is certain string outputted and redirect if string entered as “tagdata” parameter is equal to string outputted.
Here is the code using which you would be able test if {exp:qeery} tag has outputted a string “1” and redirect if it has outputted this string (checking if string “1” was outputted we are really checking if certain entry exists):
{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}
To enable this functionality is quite easy. The function I have published in my previous post should be changed as follows:
function Redirect_to()
{
global $TMPL, $FNS;
// Fetch the tagdata
$tagdata = $TMPL->tagdata;
// Fetch our parameters from the plugin tag
$location = str_replace("/", "/" ,$TMPL->fetch_param('location'));
$method = $TMPL->fetch_param('method');
$condition = $TMPL->fetch_param('tagdata');
// Perform a check to see if a full url is not given in the location parameter
if (strpos($location, 'http') !== 0 AND !(strpos($location, 'http') > 0))
{
// If we only find segments then use the create_url function to
// create our URL needed for correct re-direction
$location = $FNS->create_url($location);
}
// Clean tagdata from new line, carriage return and tab symbols
$tagdata = trim($tagdata);
$tagdata = str_replace('\n','', $tagdata);
$tagdata = str_replace('\r','', $tagdata);
$tagdata = str_replace('\t','', $tagdata);
$tagdata = str_replace('\x0B','', $tagdata);
// Perform a check if tagdata conforms to condition, or, in case condition not supplied,
// if tagdata is equal to empty string or space symbol
if ($condition === $tagdata OR (($condition === FALSE) AND (($tagdata === '') OR ($tagdata === ' '))))
{
// Perform a check to see if method parameter supplied
if ($method === FALSE)
{
// If we do not find method parameter then perform PHP redirect
$FNS->redirect($location);
}
else
{
// If we find method parameter then create redirection javascript and output it
$output = '<script type="text/javascript">location.href="'.$location.'"</script>';
$this->return_data = $output;
}
}
}
You’re right, javascript redirects suck and I’m not going to use them. I really need to get a redirect working in PHP, within conditionals. The code I have currently works, but redirects regardless of the surrounding if statements (both EE and PHP):
{if total_results == 1}<?php
if("{total_results}"==1){ ?>
{exp:redirect_to location="/casestudy/{categories show_group="4"}{category_url_title}{/categories}/{url_title}"}
<?php
}
?>
{/if}
Any help would really be appreciated on this one.
Hiya,
Thanks for the code Laisvunas. Andy sorry about all of this but I am snowed under at the moment with a lot of work and family health problems and so probably won’t be getting a chance to look at this for a while but if anyone else wants to take my plugin and add in the extra code required to make it do all these things then I am more than happy to give my consent for you to do so. Any takers? Laisvunas? 😉
If not then I promise I will look into this at the soonest moment I get.
Best wishes,
Mark
Ta. I uploaded it but still doesn’t seem to work. Here’s my code:
{exp:weblog:entries weblog="casestudy" disable="member_data|trackbacks"}
{if total_results == 1}
{exp:redirect_to location="/casestudy/{categories show_group="4"}{category_url_title}{/categories}/{url_title}"}
{/if}
{/exp:weblog:entries}
This redirects to a single entry even if total_results is greater than 1.
In fact, this code renders the word ‘YES’ to the screen so the conditional is certainly working.
{exp:weblog:entries weblog="casestudy" disable="member_data|trackbacks"}
{if total_results == 1}
YES
{!--exp:redirect_to location="/casestudy/{categories show_group="4"}{category_url_title}{/categories}/{url_title}"--}
{/if}
{/exp:weblog:entries}
Hi Andy,
having installed modified plugin you can use this code:
{exp:redirect_to location="/casestudy/{categories show_group="4"}{category_url_title}{/categories}/{url_title}" tagdata="1" method="script"}
{exp:weblog:entries weblog="casestudy" disable="member_data|trackbacks"}
{total_results}
{/exp:weblog:entries}
{/exp:redirect}
It should perform a redirect if your condition is met.
Hiya,
Just tried out the new version that you have provided Laisvunas (btw is that your name or a nickname?) and it all works brilliantly, well done.
When I get home tonight I will update the original plugin to have your name in there mentioned a lot more! I will also update the instructions in the plugin and in the first post of this thread to reflect your new add-ons and credit you there too of course! 😉
Just out of interest though (and I probably missed this in the thread somewhere) why does it work with {if} conditionals when you use a javascript location redirect but it won’t work with a PHP one?
Thanks again for the exceptionally quick work on that one and helping out like that. Much appreciated.
Best wishes,
Mark
Hi Mark,
Laisvunas is my real name. Sound strange those all names from new member-states for English ears aren’t they? :wow:
Just out of interest though (and I probably missed this in the thread somewhere) why does it work with {if} conditionals when you use a javascript location redirect but it won’t work with a PHP one?
I do not have exact answer on this but it seems that PHP redirect can only occur before http headers were sent; in contrast javascript redirect can occur anytime.
When posting modified version of the Redirect To plugin I forgot to include udated usage() function. Here is it; it’s text may also be useful when writing instructions.
function usage()
{
ob_start();
?>
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}
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
Thanks for the quick response there Laisvunas. Still not working for me I’m afraid. Here’s the exact code I’m running, complete with comments - if there’s anything obvious in there that you can spot, it’d be helpful. I’m using single quotes on my exp:redirect_to location call to cater for the categories call in there, but it does the same thing with double quotes.
{if segment_1 == ""}
{exp:weblog:entries weblog="home" dynamic="off" disable="member_data|trackbacks" limit="1"}
//do some stuff
{/exp:weblog:entries}
{if:else}
<!--GET ALL CASESTUDIES BASED ON CURRENT PAGE-->
{exp:weblog:entries weblog="casestudy" disable="member_data|trackbacks"}
<!--IF THERES ONLY ONE RESULT...-->
{if total_results == 1}
<!--...REDIRECT TO THE ACTUAL CASESTUDY PAGE-->
{exp:redirect_to location='/casestudy/{categories show_group="4"}{category_url_title}{/categories}/{url_title}' tagdata="1" method="script"}{/exp:redirect}
{/if}
{/exp:weblog:entries}
{/if}
Hi Andy,
Redirect part of your code does not comply with usage instructions of Redirect plugin. If you want to test complicated condition you shoud wrap the code expresssing that condition with {exp:redirect_to){/exp:redirect_to} tag pair. In your code {exp:redirect_to){/exp:redirect_to} tag pair does not wrap anything.
Correct code would be this:
{if segment_1 == ""}
{exp:weblog:entries weblog="home" dynamic="off" disable="member_data|trackbacks" limit="1"}
//do some stuff
{/exp:weblog:entries}
{if:else}
<!--GET ALL CASESTUDIES BASED ON CURRENT PAGE-->
<!--IF THERES ONLY ONE RESULT...-->
<!--...REDIRECT TO THE ACTUAL CASESTUDY PAGE-->
{exp:redirect_to location='/casestudy/{categories show_group="4"}{category_url_title}{/categories}/{url_title}' tagdata="1" method="script"}
{exp:weblog:entries weblog="casestudy" disable="member_data|trackbacks"}
{total_results}
{/exp:weblog:entries}
{/exp:redirect_to}
{/if}
Redirect should be performed if {exp:weblog:entries weblog="casestudy" disable="member_data|trackbacks"} tag outputs the string “1”, that is, when there is only one entry found.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.