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.
- Login as administrator, at http://localhost/joomla/administrator.
- Open Global Configuration by click Site > Global Configuration.
- Set Search Engine Friendly Urls to "yes" and Use Apache mod_rewrite to "yes".
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).
- Change "htaccess.txt" within joomla directory to be ".htaccess".
- Create "router.php" within joomla/components/com_hello. Enter following code:
<?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;
}
?>
- Now, open "hello.html.php". Update showhello() with following code:
<?
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
}
?>
- Change following line code at viewHello():
<?php $link = JRoute::_('index.php?option='. $option); ?>
with this:
<?php $link = JRoute::_('index.php?option='. $option); ?>
- Point your browser to http://localhost/joomla. We look how router works. Look all links change like this:
http://localhost/joomla/hello-world
http://localhost/joomla/news-feed
http://localhost/joomla/web-links
- Open hello world at http://localhost/joomla/hello-world
- Click one of items. You will get error. We must intepret url before.
- Open again router.php. Add following bold code:
<?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
...