Force JoomSef to understand directory structure (trailing slash)
Thursday, 24 April 2008
Ok, let me explain what do I mean with this title. I'm using Artio JoomSef component for SEO urls on Joomla 1.0.x. Link to root of my sections and categories end with /index.html
for an example:
http://www.vitez-studios.com/section/category/index.html

However, I have found out that Google bot is trying to open
http://www.vitez-studios.com/section/category/
without index file, assuming that it will retrieve different content. Of course, Google has received a lot of 404 errors which might compromise my web site indexing.
I decided to do some changes in JoomSef code. The plan was to automatically redirect all request urls which may be directories without ending slashes, to same urls with glued index.html

If you would like to add such compatibility to your JoomSef component read the following.
Please note that my changes wont add new records to JoomSef mysql tables. If no url is found in database, JoomSef will check for additional url with index.html at the end of string. If match is found, Joomla will redirect user with http error code 302, instead of responding with error code 404.open your Joomla root directory and open this file components/com_sef/sef_ext.php
Find function revert() in class sef_joomsef. Look for this code

if ($row) {
// Use the already created URL
$string = $row->newurl;

Add this code right before!
/*---------------------------
# see if url is actually a dir root
#
# By Vitez
----------------------------*/

if (! $row) {
global $mosConfig_live_site;
$isSlash = substr($req, -1) == '/';
$req .= $isSlash ? '' : '/';
$req .= $sefConfig->addFile;
// try to load again with trailing slash
$sql = "SELECT * FROM #__redirection WHERE `oldurl` = '".$req."' AND `newurl` != '' LIMIT 1";
$database->setQuery($sql);

// Try to use cache
if ($sefConfig->useCache) {
$row = $jsCache->getNonSefUrl($req);
} else {
$row = null;
}
if ($row) {
// Cache worked
$fromCache = true;
}
else {
// URL isn't in cache or cache disabled
$fromCache = false;
$row = null;
$database->loadObject($row);
}

// provjeri da li je zatrazen dir bez trailing slasha
// u tom slucaju redirectaj sa trailing slashom

if ($row && ! $isSlash)
{
$f = $d = "";
if (!headers_sent($f,$d))
{
header("Location: {$mosConfig_live_site}/".$req);
exit();
}
else
{
die("SEO error -> cannot redirect. Headers already sent at line {$d} in file {$f}");
}
}

// fake it
$fromCache = true;
}
// END.Vitez

Thats it, try it. If you experience problems feel free to contact me ;)