Skip to content
This repository has been archived by the owner on Feb 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #347 from joomlatools/feature/346-404redirect-plugin
Browse files Browse the repository at this point in the history
Create 404 re-direct plugin
  • Loading branch information
allanpilarca authored Mar 16, 2018
2 parents 5ac999c + 8283b62 commit 87014dc
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Phinx\Migration\AbstractMigration;

class InstallPlgSystem404 extends AbstractMigration
{
public function up()
{
$sql = <<<EOL
INSERT INTO `extensions` (`extension_id`, `name`, `type`, `element`, `folder`, `client_id`, `enabled`, `access`, `protected`, `manifest_cache`, `params`, `custom_data`, `system_data`, `checked_out`, `checked_out_time`, `ordering`, `state`)
VALUES
(450, 'plg_system_404', 'plugin', '404', 'system', 0, 1, 1, 0, '{}', '{}', '', '', 0, '0000-00-00 00:00:00', 0, 0);
EOL;

$this->execute($sql);
}

public function down()
{
$this->execute("DELETE FROM `extensions` WHERE `extensions`.`name` = 'plg_system_404'");
}
}
44 changes: 44 additions & 0 deletions lib/plugins/system/404/404.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* @package System - 404
* @copyright Copyright (C) 2018 Timble CVBA. (http://www.timble.net)
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
* @link http://www.joomlatools.com
*/

defined('_JEXEC') or die;

class PlgSystem404 extends JPlugin
{
public function __construct(&$subject, $config)
{
parent::__construct($subject, $config);

if (JFactory::getApplication()->isSite())
{
// Set the JError handler for E_ERROR to be the class' handleError method.
JError::setErrorHandling(E_ERROR, 'callback', array('PlgSystem404', 'handleError'));
}
}

public static function handleError(JException $error)
{
$app = JFactory::getApplication();

if ($app->get('sef') && (int) $error->getCode() == 404)
{
$regex = '/\/[\d]+\-/';
$current = JUri::current();

// Match url having "/xxx-" format, where xxx is a number.
if (preg_match($regex, $current))
{
// Redirect with-id to no-id url
$redirect = preg_replace($regex, '/', $current);
$app->redirect($redirect);
}
}

JErrorPage::render($error);
}
}
19 changes: 19 additions & 0 deletions lib/plugins/system/404/404.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<extension version="3.1" type="plugin" group="system" method="upgrade">
<name>plg_system_404</name>
<author>Joomlatools</author>
<creationDate>March 2018</creationDate>
<copyright>Copyright (C) 2018 Timble CVBA (http://www.timble.net)</copyright>
<license>GNU GPLv3 - http://www.gnu.org/licenses/gpl.html</license>
<authorEmail>support@joomlatools.com</authorEmail>
<authorUrl>www.joomlatools.com</authorUrl>
<version>1.0.0</version>
<description>PLG_SYSTEM_404_DESCRIPTION</description>
<files>
<filename plugin="404">404.php</filename>
</files>
<languages>
<language tag="en-GB">en-GB.plg_system_404.ini</language>
<language tag="en-GB">en-GB.plg_system_404.sys.ini</language>
</languages>
</extension>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
; Copyright © 2018 Timble CVBA. (http://www.timble.net)
; GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
; Note : All ini files need to be saved as UTF-8

PLG_SYSTEM_404="System - 404"
PLG_SYSTEM_404_DESCRIPTION="This will redirect with-id to no-id url."
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
; Copyright © 2018 Timble CVBA. (http://www.timble.net)
; GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
; Note : All ini files need to be saved as UTF-8

PLG_SYSTEM_404="System - 404"
PLG_SYSTEM_404_DESCRIPTION="This will redirect with-id to no-id url."

0 comments on commit 87014dc

Please sign in to comment.