Skip to content

Commit

Permalink
SEF: Enforcing correct SEF URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackwar committed Feb 21, 2024
1 parent bd1f055 commit 6ae36cb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
2 changes: 2 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,7 @@

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_ENFORCESEF_DESCRIPTION="When the URL contains non-SEF parts or the page is normally reachable via a different URL, this option forces the browser to redirect to the correct URL."
PLG_SEF_ENFORCESEF_LABEL="Disallow non-SEF URLs"
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="enforcesef"
type="list"
label="PLG_SEF_ENFORCESEF_LABEL"
description="PLG_SEF_ENFORCESEF_DESCRIPTION"
layout="joomla.form.field.radio.switcher"
default="0"
filter="boolean"
>
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>
</fieldset>
</fields>
</config>
Expand Down
33 changes: 33 additions & 0 deletions plugins/system/sef/src/Extension/Sef.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,39 @@
*/
final class Sef extends CMSPlugin
{
/**
* Application object.
*
* @var \Joomla\CMS\Application\CMSApplication
* @since __DEPLOY_VERSION__
*/
protected $app;

/**
* Enforce the SEF URL with a redirect
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function onAfterRoute()
{
$input = $this->app->getInput();

// We only want to redirect on GET requests
if (!$this->app->isClient('site') || !$this->params->get('enforcesef') || $input->getMethod() !== 'GET') {
return;
}

$origUri = Uri::getInstance();
$route = $origUri->toString(['path','query']);
$newRoute = Route::_($input->getArray(), false);

if ($route !== $newRoute) {
$this->app->redirect($newRoute, 301);
}
}

/**
* Add the canonical uri to the head.
*
Expand Down

0 comments on commit 6ae36cb

Please sign in to comment.