joomlagerman / joomla

The J!German translation team provides German translation for Joomla!
https://www.jgerman.de
GNU General Public License v2.0
25 stars 46 forks source link

[5.2] SEF: Enforce suffix #3243

Closed jgerman-bot closed 2 months ago

jgerman-bot commented 2 months ago

New language relevant PR in upstream repo: https://github.com/joomla/joomla-cms/pull/42850 Here are the upstream changes:

Click to expand the diff! ```diff diff --git a/administrator/language/en-GB/plg_system_sef.ini b/administrator/language/en-GB/plg_system_sef.ini index 085b81a0a0ce4..60f465d581eb3 100644 --- a/administrator/language/en-GB/plg_system_sef.ini +++ b/administrator/language/en-GB/plg_system_sef.ini @@ -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.
Note: 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." diff --git a/plugins/system/sef/sef.xml b/plugins/system/sef/sef.xml index 467cac9fe0166..1ef51a3667ace 100644 --- a/plugins/system/sef/sef.xml +++ b/plugins/system/sef/sef.xml @@ -31,6 +31,19 @@ validate="url" /> + + + + + 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(); @@ -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) { + // 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 * ```