Beware this content is over 8 years old and considered stale. It may no longer be accurate and/or reflect the understanding of the author but remains here for reference only. Please keep this in mind as you use this content.

A few days ago I made a post relating to an issue I came across with Joomla! during a move from running it in basic SEF mode, which utilises the aliases given when creating categories and articles in the Joomla administration/back-end, to the full/advanced SEF mode which utilises both the aliases and Apache’s mod_rewrite to remove the index.php from the URL. Actual implementation requires a minor modification of the .htaccess within the root Jooma directory, which I hope to cover in a subsequent post.

For example:
Basic SEF: artofsimplicity.co.uk/index.php/category/an-article-headline
Full SEF: artofsimplicity.co.uk/category/an-article-headline

The script we started off with was a simple one that tried to determine the URL that the user had used or clicked on in order to access a particular resource on the website. When the resource is not found Joomla! automatically displays a standard 404 page that says something similar, that it was not found. In order to save some users from a vertible dead-end and subsequently lost traffic, I tried to hack together a few lines of PHP to save that stranded viewer and link them through to the originally linked content.

The idea was simply, that the original links that were already being cached and were now out of date, contained a reference to the index.php which provided templating information for the dynamically generating content. In comparison, the fully SEF URLs removed the index.php from the URL entirely, the result being that old URLs would no longer be able to retrieve the original content. Therefore, I simply aimed to retrieve the destination URL, remove the index.php from it and reconstruct a new URL for the visitor to click through to information they were after.

The culmination of some tinkering, leaves me with the following that works quite well so far. A break down should follow.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
$brokenurl = $_SERVER["REQUEST_URI"];
$findme = 'index.php/';
$position = strpos($brokenurl, $findme);
if ($position === false) {
print "
<div class=\"search\">
# Sorry!
It appears as though there was a problem retrieving the page you requested. Please try using the search engine below:
<form name=\"searchForm\" method=\"post\" action=\"/news-archive\" id=\"searchForm\">
<input type=\"text\" class=\"inputbox\" value=\"\" maxlength=\"20\" size=\"15\" id=\"search_searchword\" name=\"searchword\"/>
<button class=\"button\" onclick=\"this.form.submit()\" name=\"Search\">![](\)</button>
<input type=\"hidden\" value=\"all\" name=\"searchphrase\"/>
<input type=\"hidden\" value=\"newest\" name=\"ordering\"/>
<input type=\"hidden\" value=\"search\" name=\"task\"/>
</form>
</div>
";
} else {
$p = '/\/index.php\//';
$r = '';
$o = preg_replace($p, $r, $brokenurl);
$newurl = "https://artofsimplicity.co.uk/$o";
print "
<script type=\"text/JavaScript\">
<!--
setTimeout(\"location.href = '$newurl';\",10000);
-->
</script>
We have detected that the page you requested has been moved to the following location:
[$newurl]($newurl)
You can click the above link now or wait and you will be redirected to the above URL within 10 seconds.
";
}
?>

For now, a note on a few of the things I have added.

  1. Should the destination link not be a SEF related issue but simply some content has been removed or deleted entirely, a search engine is displayed to allow users to attempt a site-wide search.
  2. If the destination is a SEF related issue, as defined by the presence of index.php in the URL. It will create the link and with a few lines of JavaScript, automatically attempt to connect the user to the content they were looking for.

I can see this being by no means fool-proof, so I would welcome other ideas or alternatives to solvinging this particular problem.