Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] SEF: Enforcing correct SEF URL #42854

Open
wants to merge 8 commits into
base: 5.2-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch '5.1-dev' of https://github.com/joomla/joomla-cms into 5…
….1-router-sef

# Conflicts:
#	administrator/language/en-GB/plg_system_sef.ini
#	plugins/system/sef/sef.xml
#	plugins/system/sef/src/Extension/Sef.php
  • Loading branch information
Hackwar committed Mar 9, 2024
commit 45f016b0fbb1a7825c7e32a5a77bdb0e23e67e76
7 changes: 7 additions & 0 deletions administrator/language/en-GB/plg_system_sef.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@ PLG_SEF_DOMAIN_DESCRIPTION="If your site can be accessed through more than one d
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"
Hackwar marked this conversation as resolved.
Show resolved Hide resolved
PLG_SEF_INDEXPHP_DESCRIPTION="This option enables a stricter handling of 'index.php' in URLs when 'Use URL Rewriting' is enabled in Global Configuration. It will remove 'index.php' if a URL still contains it and redirect incoming requests with 'index.php' to the version without the 'index.php'."
PLG_SEF_INDEXPHP_LABEL="Strict handling of index.php"
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"
26 changes: 26 additions & 0 deletions plugins/system/sef/sef.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>

<field
name="indexphp"
type="radio"
label="PLG_SEF_INDEXPHP_LABEL"
description="PLG_SEF_INDEXPHP_DESCRIPTION"
layout="joomla.form.field.radio.switcher"
default="0"
filter="boolean"
>
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>

<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
82 changes: 68 additions & 14 deletions plugins/system/sef/src/Extension/Sef.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,89 @@
final class Sef extends CMSPlugin implements SubscriberInterface
{
/**
* Application object.
* Returns an array of CMS events this plugin will listen to and the respective handlers.
*
* @var \Joomla\CMS\Application\CMSApplication
* @since __DEPLOY_VERSION__
* @return array
*
* @since 5.1.0
*/
protected $app;
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',
'onAfterRoute' => 'onAfterRoute',
'onAfterDispatch' => 'onAfterDispatch',
'onAfterRender' => 'onAfterRender',
];
}

/**
* Enforce the SEF URL with a redirect
* After initialise router.
*
* @return void
*
* @since __DEPLOY_VERSION__
* @since 5.1.0
*/
public function onAfterInitialiseRouter(AfterInitialiseRouterEvent $event)
{
$app = $this->getApplication();

if (
is_a($event->getRouter(), SiteRouter::class)
&& $app->get('sef')
&& !$app->get('sef_suffix')
&& $this->params->get('trailingslash')
) {
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);
}
}
}

/**
* OnAfterRoute listener
*
* @return void
*
* @since 5.1.0
*/
public function onAfterRoute()
{
$input = $this->app->getInput();
$app = $this->getApplication();

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

$origUri = Uri::getInstance();
$route = $origUri->toString(['path','query']);
$newRoute = Route::_($input->getArray(), false);
// Enforce removing index.php with a redirect
if ($app->get('sef_rewrite') && $this->params->get('indexphp')) {
$this->removeIndexphp();
}

if ($route !== $newRoute) {
$this->app->redirect($newRoute, 301);
// Check for trailing slash
if ($app->get('sef') && !$app->get('sef_suffix') && $this->params->get('trailingslash')) {
$this->enforceTrailingSlash();
}

// Enforce SEF URLs
if ($this->params->get('enforcesef') && $app->getInput()->getMethod() !== 'GET') {
$origUri = Uri::getInstance();
$route = $origUri->toString(['path','query']);
$newRoute = Route::_($app->getInput()->getArray(), false);

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

Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.