Search Engine Friendly urls in Joomla


I just realized how to accomplish SEF urls in Joomla, first go Global Configuration -> Site and set all the SEO radio buttons to Yes.

Now to link to an article in your PHP / HTML code you need to set an alias for it when you create it, then you could do like this in the code:

<a href="<?php echo JRoute::_('index.php?option=com_content&view=article&id='.$article->id.'-'.$article->alias) ?>">
  <h2><?php echo $article->title ?></h2>
</a>

Not much to it!

Update: Don’t take anything for granted when it comes to Joomla. I just created a section listing of articles and suddenly the read more links link to a 404, wonderful…

So first I have to make sure I fetch the teasers like this:

SELECT cont.*, cat.alias AS cat_alias, sect.alias AS sect_alias FROM jos_content cont
LEFT JOIN jos_categories AS cat ON cont.catid = cat.id
LEFT JOIN jos_sections AS sect ON cont.sectionid = sect.id 
WHERE cont.$id_type = {$this->getArr['id']} 
AND cont.alias NOT LIKE 'contact%' ORDER BY cont.created DESC LIMIT $offset , $limit

That way we can define a echoContLink function which is responsible for outputting the read more URLs:

function echoContLink($article){
	if($article->catid == 1)
		$this->echoBlogLink($article->created_by);
	else{
		if(!empty($article->cat_alias) && !empty($article->sect_alias))
			echo "{$article->sect_alias}/{$article->cat_alias}/".$article->id.'-'.$article->alias;
		else
			echo JRoute::_('index.php?option=com_content&view=article&id='.$article->id.'-'.$article->alias);
	}
}

If we don’t have either of cat_alias and sect_alias we do it the normal broken way (Joomla default). If we have them we use them to create the URL. Needless to say all of this breaks down if SEFs are not turned on in the Joomla backend.

Related Posts

Tags: , , ,