Joomla $app->redirect() in template index.php causing a redirect loop

TEN Design picture TEN Design · Jun 23, 2013 · Viewed 9.4k times · Source

I am attempting to redirect in a template index.php i am building and getting a redirect loop. Am I missing something? What I am attempting to do is redirect the default page depending on the entry URL as well as certain template styles. My template code looks something like this...

config.php

<?php
//joomla configuration
JLoader::import('joomla.filesystem.file');
JHtml::_('behavior.framework', true);
JHtml::_('jquery.framework');
$app = JFactory::getApplication();
$config = JFactory::getConfig();
$doc = JFactory::getDocument();

//load template style sheets and scripts
$doc->addStyleSheet($this->baseurl.'/media/jui/css/bootstrap.min.css');
$doc->addStyleSheet($this->baseurl.'/media/jui/css/bootstrap-responsive.min.css');
$doc->addStyleSheet('templates/' . $this->template . '/css/template.css');
$doc->addScript('templates/' . $this->template . '/javascript/template.js');

//sidebar configuration - determines the span classes for the sidebar and main component area
$component = $this->params->get('component-span');
if(!$this->countModules('sidebar')):
    $component = 12;
endif;
$sidebar = 12 - $component;
$sidebar_pos = $this->params->get('sidebar-pos');

//navbar inverse
if($this->params->get('navbar-inverse') == 'TRUE'):
    $inverse = ' navbar-inverse';
endif;

//multisite configuration - determines which template params and menu module to display depending on the base URL
$domain = $_SERVER["SERVER_NAME"];
$primary = $this->params->get('site-domain');
$sub1= $this->params->get('domain1-domain');
$sub2= $this->params->get('domain2-domain');
$sub3= $this->params->get('domain3-domain');
$sub4= $this->params->get('domain4-domain');
$sub5= $this->params->get('domain5-domain');
$menuid = $app->getMenu(); 

if($domain == $primary):
    $logo = $this->params->get('logo');
    $title = $this->params->get('site-title');
    $slogan = $this->params->get('site-slogan');
    $menu = '<jdoc:include type="modules" name="menu" />';
elseif($domain == $sub1):
    $logo = $this->params->get('domain1-logo');
    $title = $this->params->get('domain1-title');
    $slogan = $this->params->get('domain1-slogan');
    $menu = '<jdoc:include type="modules" name="menu-1" />';
    $menuitemid = $this->params->get('domain1-menuid');
    $menuitem = $menuid->getItem($menuitemid);
elseif($domain == $sub2):
    $logo = $this->params->get('domain2-logo');
    $title = $this->params->get('domain2-title');
    $slogan = $this->params->get('domain2-slogan');
    $menu = '<jdoc:include type="modules" name="menu-2" />';
    $itemid = $this->params->get('domain2-menuid');
elseif($domian == $sub3):
    $logo = $this->params->get('domain3-logo');
    $title = $this->params->get('domain3-title');
    $slogan = $this->params->get('domain3-slogan');
    $menu = '<jdoc:include type="modules" name="menu-3" />';
    $itemid = $this->params->get('domain3-menuid');
elseif($domain == $sub4):
    $logo = $this->params->get('domain4-logo');
    $title = $this->params->get('domain4-title');
    $slogan = $this->params->get('domain4-slogan');
    $menu = '<jdoc:include type="modules" name="menu-4" />';
    $itemid = $this->params->get('domain4-menuid');
elseif($domain == $sub5):
    $logo = $this->params->get('domain5-logo');
    $title = $this->params->get('domain5-title');
    $slogan = $this->params->get('domain5-slogan');
    $menu = '<jdoc:include type="modules" name="menu-5" />';
    $itemid = $this->params->get('domain5-menuid');
endif;


?>

index.php

<?php
defined('_JEXEC') or die;
include($this->baseurl.'templates/'.$this->template.'/includes/config.php');
$default_page = new JURI($menuitem->link);
print($domain.'<br />'.$primary.'<br />'.$default_page);

if ($domain != $primary):
    $link  = $default_page;
    $msg   = 'Testing Redirect'; 
    $app->redirect($link, $msg);
endif;
?>

<!DOCTYPE html>
<html>
<head>
    <?php $this->setTitle($title.' - '.$this->getTitle()); ?>
    <jdoc:include type="head" />
</head>

Answer

Emerson Rocha picture Emerson Rocha · Feb 3, 2014

This is occurring because $domain != $primary is failing. The way that you get $domain maybe is the root of cause.

Look at PHP $_SERVER['HTTP_HOST'] vs. $_SERVER['SERVER_NAME'], am I understanding the man pages correctly? and be sure that is not this the problem.

Also, Joomla have JUri package that help with this. If you are working with Joomla, is a good idea use it.