Alexander Conroy

Better SEF URL’s for Blogs and News – Joomla! Hack

All credit to this article goes to Ahmad from Alfy Studio. Great work!!

sh404SEF is a Joomla! extension helps you creating better SEF URLs. It’s full of features and parameters that helps you to acheieve the format you want for your Joomla! website. On July 24th 2008 the JED editors announced that sh404SEF has been added to the editors’ pick. Development cycle continued and sh404SEF became one of the must have extensions.

Bascially an article will have a URL similar to this :

“http://localhost/index.php?option=com_content&view=article&id=19”

With joomla built-in SEF URLs enabled the URL will be similar to :

“http://localhost/category-name/19-article-title”

That looks much better and more human readable. Better for SEO too! It’s effective yet very limited! We don’t have much control over the URL format. For example there is no way to hide the article-id prefixed to the title. It doesn’t tell the visitor any useful thing; why to use it?

For a lot of things we need extensions to handle SEF URLs. We need more control and more options. When using sh404SEF an article should have a URL similar to this one :

“http://localhost/category-name/article-title.html”

Similar to Joomla’s built-in URL but better. You can set the parameters to show/hide the section/category, use an extension like .html and more. There is a nice feature in sh404SEF helps adding a unique-id for the URL. When enabled the URL should look similar to :

http://localhost/2008081219/category-name/article-title.html”

That unique id is created using the date of creation (yyyy-mm-dd) added to the article id. Well that could be of great value with minor modification to modify the output to something better! Take a look at the following :

“http://localhost/category-name/2008/08/12/article-title.html”

Even better, This URL tells a story! Obviously that’s a date of creation and not just a weird number! To create such a format we need to modify sh404SEF files. To get started :

  • Install sh404SEF.
  • Rename the file in your website root called htaccess.txt to .htaccess
  • From SEO Settings in Joomla’s Global Configuration enable both Search Engine Friendly URLs andUse Apache mod_rewrite
  • Go to sh404SEF control panel, make sure you’re using the extended display by clicking on the blue notice message on the right.
  • Open sh404SEF configuration :
    • on Main set Unique ID to Yes
    • on Plugins set show-section/show-category to what suits you
    • on Advanced set Rewriting mode to with .htaccess ( mod_rewrite )
    • Save settings.

Lets digg into the code. Start by editing the file:

/components/com_sh404sef/sef_ext/com_content.php

That’s the file responsible for handling the URLs for the contents. Find and delete the following code:

01.// V 1.2.4.j 2007/04/11 : numerical ID, on some categories only
02.if ($sefConfig->shInsertNumericalId && isset($sefConfig->shInsertNumericalIdCatList)
03.&& !empty($id) && ($view == ‘article’)) {
04.
05.    $q = ‘SELECT id, catid, created FROM #__content WHERE id = ‘.$id;
06.    $database->setQuery($q);
07.    if (shTranslateUrl($option, $shLangName)) // V 1.2.4.m
08.    $contentElement = $database->loadObject( );
09.    else $contentElement = $database->loadObject( false);
10.    if ($contentElement) {
11.        $foundCat = array_search($contentElement->catid, $sefConfig->shInsertNumericalIdCatList);
12.        if (($foundCat !== null && $foundCat !== false)
13.        || ($sefConfig->shInsertNumericalIdCatList[0] == ”))  { // test both in case PHP < 4.2.0
14.            $shTemp = explode(‘ ‘, $contentElement->created);
15.            $title[] = str_replace(‘-‘,”, $shTemp[0]).$contentElement->id;
16.        }
17.    }
18.}

Save and close the file. Open:

/components/com_sh404sef/sef_ext.php

Find this line :

1.if (isset($row->category)) {
2.    $title[] = $row->category;
3.}

Insert this code after :

01.// V 1.2.4.j 2007/04/11 : numerical ID, on some categories only
02.if ($sefConfig->shInsertNumericalId && isset($sefConfig->shInsertNumericalIdCatList)
03.&& !empty($id) && ($view == ‘article’)) {
04.
05.    $q = ‘SELECT id, catid, created FROM #__content WHERE id = ‘.$id;
06.    $database->setQuery($q);
07.    if (shTranslateUrl($option, $shLangName)) // V 1.2.4.m
08.    $contentElement = $database->loadObject( );
09.    else $contentElement = $database->loadObject( false);
10.    if ($contentElement) {
11.        $foundCat = array_search($contentElement->catid, $sefConfig->shInsertNumericalIdCatList);
12.        if (($foundCat !== null && $foundCat !== false)
13.        || ($sefConfig->shInsertNumericalIdCatList[0] == ”))  { // test both in case PHP < 4.2.0
14.            $shTemp = explode(‘ ‘, $contentElement->created);
15.            $title[] = str_replace(‘-‘,’/’, $shTemp[0]).”/”;
16.        }
17.    }
18.}

We’ve only modified the last line from the code we removed from the first file.

  1. Moved the code to be appeneded after the /section-name/category-name/ in the URL
  2. The ( – ) are replaced with ( / ) using str_replace
  3. Removed the itemid contentElement->id
  4. Added the ( / ) to the end.

Save the file and now purge SEF URLs you have from the sh404SEF control panel. Try reloading your website and see how the URLs look now

If you’ve better ideas or modifications please share it! Have a nice day!