Skip to content

Commit

Permalink
[5.1] SEF: Implementing trailing slash behavior (#42702)
Browse files Browse the repository at this point in the history
* SEF: Implementing trailing slash behavior
* Use 301 redirects
  • Loading branch information
Hackwar committed Feb 28, 2024
1 parent af73758 commit 96b5735
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 1 deletion.
5 changes: 5 additions & 0 deletions administrator/language/en-GB/plg_system_sef.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@

PLG_SEF_DOMAIN_DESCRIPTION="If your site can be accessed through more than one domain enter the preferred (sometimes referred to as canonical) domain here. <br><strong>Note:</strong> https://example.com and https://www.example.com are different domains."
PLG_SEF_DOMAIN_LABEL="Site Domain"
PLG_SEF_TRAILINGSLASH_DESCRIPTION="Force Joomla to only use URLs with or without trailing slash. When set, this will force the right URL with redirects and is only applied when 'Add suffix to URL' is disabled."
PLG_SEF_TRAILINGSLASH_LABEL="Trailing slash for URLs"
PLG_SEF_TRAILINGSLASH_OPTION_NONE="No change"
PLG_SEF_TRAILINGSLASH_OPTION_NO_SLASH="Enforce URLs without trailing slash"
PLG_SEF_TRAILINGSLASH_OPTION_SLASH="Enforce URLs with trailing slash"
PLG_SEF_XML_DESCRIPTION="Adds SEF support to links in the document. It operates directly on the HTML and does not require a special tag."
PLG_SYSTEM_SEF="System - SEF"
13 changes: 13 additions & 0 deletions plugins/system/sef/sef.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@
filter="url"
validate="url"
/>

<field
name="trailingslash"
type="list"
label="PLG_SEF_TRAILINGSLASH_LABEL"
description="PLG_SEF_TRAILINGSLASH_DESCRIPTION"
default="0"
filter="option"
>
<option value="0">PLG_SEF_TRAILINGSLASH_OPTION_NONE</option>
<option value="1">PLG_SEF_TRAILINGSLASH_OPTION_NO_SLASH</option>
<option value="2">PLG_SEF_TRAILINGSLASH_OPTION_SLASH</option>
</field>
</fieldset>
</fields>
</config>
Expand Down
132 changes: 131 additions & 1 deletion plugins/system/sef/src/Extension/Sef.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@

namespace Joomla\Plugin\System\Sef\Extension;

use Joomla\CMS\Event\Router\AfterInitialiseRouterEvent;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Router\Router;
use Joomla\CMS\Router\SiteRouter;
use Joomla\CMS\Uri\Uri;
use Joomla\Event\SubscriberInterface;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand All @@ -23,8 +27,66 @@
*
* @since 1.5
*/
final class Sef extends CMSPlugin
final class Sef extends CMSPlugin implements SubscriberInterface
{
/**
* Application object.
*
* @var \Joomla\CMS\Application\CMSApplication
* @since __DEPLOY_VERSION__
*/
protected $app;

/**
* Returns an array of CMS events this plugin will listen to and the respective handlers.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public static function getSubscribedEvents(): array
{
/**
* Note that onAfterInitialise must be the first handlers to run for this
* plugin to operate as expected. These handlers load compatibility code which
* might be needed by other plugins
*/
return [
'onAfterInitialiseRouter' => 'onAfterInitialiseRouter',
'onAfterDispatch' => 'onAfterDispatch',
'onAfterRender' => 'onAfterRender',
];
}

/**
* After initialise router.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function onAfterInitialiseRouter(AfterInitialiseRouterEvent $event)
{
if (
!is_a($event->getRouter(), SiteRouter::class)
|| !$this->app->get('sef')
|| $this->app->get('sef_suffix')
|| !$this->params->get('trailingslash')
) {
return;
}

if ($this->params->get('trailingslash') == 1) {
// Remove trailingslash
$event->getRouter()->attachBuildRule([$this, 'removeTrailingSlash'], SiteRouter::PROCESS_AFTER);
} elseif ($this->params->get('trailingslash') == 2) {
// Add trailingslash
$event->getRouter()->attachBuildRule([$this, 'addTrailingSlash'], SiteRouter::PROCESS_AFTER);
}

$event->getRouter()->attachParseRule([$this, 'enforceTrailingSlash'], SiteRouter::PROCESS_BEFORE);
}

/**
* Add the canonical uri to the head.
*
Expand Down Expand Up @@ -188,6 +250,74 @@ function ($match) use ($base, $protocols) {
$this->getApplication()->setBody($buffer);
}

/**
* Remove any trailing slash from URLs built in Joomla
*
* @param Router &$router Router object.
* @param Uri &$uri Uri object.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function removeTrailingSlash(&$router, &$uri)
{
$path = $uri->getPath();

if (substr($path, -1) == '/') {
$uri->setPath(substr($path, 0, -1));
}
}

/**
* Add trailing slash to URLs built in Joomla
*
* @param Router &$router Router object.
* @param Uri &$uri Uri object.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function addTrailingSlash(&$router, &$uri)
{
$path = $uri->getPath();

if (substr($path, -1) !== '/') {
$uri->setPath($path . '/');
}
}

/**
* Redirect to a URL with or without trailing slash
*
* @param Router &$router Router object.
* @param Uri &$uri Uri object.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function enforceTrailingSlash(&$router, &$uri)
{
// We only want to redirect on GET requests
if ($this->app->getInput()->getMethod() != 'GET') {
return;
}

$originalUri = Uri::getInstance();

if ($this->params->get('trailingslash') == 1 && substr($originalUri->getPath(), -1) == '/' && $originalUri->toString() != Uri::root()) {
// Remove trailingslash
$originalUri->setPath(substr($originalUri->getPath(), 0, -1));
$this->app->redirect($originalUri->toString(), 301);
} elseif ($this->params->get('trailingslash') == 2 && substr($originalUri->getPath(), -1) != '/') {
// Add trailingslash
$originalUri->setPath($originalUri->getPath() . '/');
$this->app->redirect($originalUri->toString(), 301);
}
}

/**
* Check the buffer.
*
Expand Down

0 comments on commit 96b5735

Please sign in to comment.