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: Enforce suffix #42850

Merged
merged 14 commits into from
Aug 15, 2024
Merged
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,6 +5,8 @@

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_ENFORCESUFFIX_DESCRIPTION="When enabled and \"Add Suffix to URL\" is also enabled, URLs without suffix will be redirected to the correct one. From version 6.0 onwards this will be the standard behavior and not an option anymore."
PLG_SEF_ENFORCESUFFIX_LABEL="Enforce a suffix by redirect"
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_STRICTROUTING_DESCRIPTION="Prevent the router from accepting a lot of URLs which are deemed as duplicates and will automatically redirect to the correct URL with a 301."
Expand Down
13 changes: 13 additions & 0 deletions plugins/system/sef/sef.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@
validate="url"
/>

<field
name="enforcesuffix"
type="list"
label="PLG_SEF_ENFORCESUFFIX_LABEL"
description="PLG_SEF_ENFORCESUFFIX_DESCRIPTION"
layout="joomla.form.field.radio.switcher"
default="0"
filter="boolean"
>
<option value="0">JNO</option>
<option value="1">JYES</option>
</field>

<field
name="indexphp"
type="radio"
Expand Down
47 changes: 47 additions & 0 deletions plugins/system/sef/src/Extension/Sef.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public function onAfterRoute()
$this->enforceTrailingSlash();
}

// Enforce adding a suffix with a redirect
if ($app->get('sef') && $app->get('sef_suffix') && $this->params->get('enforcesuffix')) {
$this->enforceSuffix();
}

// Enforce SEF URLs
if ($this->params->get('strictrouting') && $app->getInput()->getMethod() == 'GET') {
$this->enforceSEF();
Expand Down Expand Up @@ -279,6 +284,48 @@ function ($match) use ($base, $protocols) {
$this->getApplication()->setBody($buffer);
}

/**
* Enforce the URL suffix with a redirect
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function enforceSuffix()
{
$origUri = Uri::getInstance();
$route = $origUri->getPath();

if (substr($route, -9) === 'index.php' || substr($route, -1) === '/') {
// We don't want suffixes when the URL ends in index.php or with a /
return;
}

$suffix = pathinfo($route, PATHINFO_EXTENSION);
$nonSEFSuffix = $origUri->getVar('format');

if ($nonSEFSuffix && $suffix !== $nonSEFSuffix) {
Hackwar marked this conversation as resolved.
Show resolved Hide resolved
// There is a URL query parameter named "format", which isn't the same to the suffix
$pathWithoutSuffix = ($suffix !== '') ? substr($route, 0, -(\strlen($suffix) + 1)) : $route;

$origUri->delVar('format');
$origUri->setPath($pathWithoutSuffix . '.' . $nonSEFSuffix);
$this->getApplication()->redirect($origUri->toString(), 301);
}

if ($suffix && $suffix == $nonSEFSuffix) {
// There is a URL query parameter named "format", which is identical to the suffix
$origUri->delVar('format');
$this->getApplication()->redirect($origUri->toString(), 301);
}

if (!$suffix) {
// We don't have a suffix, so we default to .html at the end
$origUri->setPath($route . '.html');
$this->getApplication()->redirect($origUri->toString(), 301);
}
}

/**
* Enforce removal of index.php with a redirect
*
Expand Down