You know, standard url at Joomla like this http://jomla-site/index.php?option=com_contact&task=view&id=123. It appear as long GET string. For your visitors and search engine, it doen't make happy.
It will be very helpful if our like like this, http://jomla-site/contact/view/123. Do you agree? This tutorial focus how to generate friendly links.
This tutorial is series from Joomla! tutorial before. So, please read Joomla! tutorial before.
mod_rewrite must be installed in your Apache. If you don't know about how to install module mod_rewrite, you can read tutorial about mod_rewrite in this site (see category: Apache).
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
function HelloBuildRoute(&$query)
{
$segments = array();
if(isset($query['task']))
{
$segments[] = $query['task'];
unset($query['task']);
};
if(isset($query['id']))
{
$segments[] = $query['id'];
unset($query['id']);
};
return $segments;
}
?>
<?
function showHello($rows, $option)
{ ?>
<table>
<?php
foreach($rows as $row)
{
$link = JRoute::_( 'index.php?option='.$option.'&id='.$row->id . '&task=view' );
echo '
<tr>
<td>
<a href="'. $link .'">'. $row->message .'</a>
</td>
</tr>
';
}
?>
</table>
<?php
}
?>
<?php $link = JRoute::_('index.php?option='. $option); ?>
with this:
<?php $link = JRoute::_('index.php?option='. $option); ?>
http://localhost/joomla/hello-world http://localhost/joomla/news-feed http://localhost/joomla/web-links
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
function HelloBuildRoute(&$query)
{
$segments = array();
if(isset($query['task']))
{
$segments[] = $query['task'];
unset($query['task']);
};
if(isset($query['id']))
{
$segments[] = $query['id'];
unset($query['id']);
};
return $segments;
}
function HelloParseRoute($segments)
{
$vars = array();
$vars['task'] = $segments[0];
$vars['id'] = $segments[1];
return $vars;
}
?>
Finish, urls like this must work:
http://localhost/joomla/hello-world/view/1 http://localhost/joomla/hello-world/view/2 http://localhost/joomla/hello-world/view/3 ...