Skip to content

Commit

Permalink
[5.1] SEF: Implementing index.php behavior (#42704)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackwar committed Feb 29, 2024
1 parent e3000bb commit 8da0b7c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 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,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_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"
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="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"
Expand Down
65 changes: 52 additions & 13 deletions plugins/system/sef/src/Extension/Sef.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,30 @@ public static function getSubscribedEvents(): array
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')
is_a($event->getRouter(), SiteRouter::class)
&& $this->app->get('sef_rewrite')
&& $this->params->get('indexphp')
) {
return;
// Enforce removing index.php with a redirect
$event->getRouter()->attachParseRule([$this, 'removeIndexphp'], SiteRouter::PROCESS_BEFORE);
}

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

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

/**
Expand Down Expand Up @@ -250,6 +257,38 @@ function ($match) use ($base, $protocols) {
$this->getApplication()->setBody($buffer);
}

/**
* Enforce removal of index.php with a redirect
*
* @param Router &$router Router object.
* @param Uri &$uri Uri object.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function removeIndexphp(&$router, &$uri)
{
// We only want to redirect on GET requests
if ($this->app->getInput()->getMethod() !== 'GET') {
return;
}

$origUri = Uri::getInstance();

if (substr($origUri->getPath(), -9) === 'index.php') {
// Remove trailing index.php
$origUri->setPath(substr($origUri->getPath(), 0, -9));
$this->app->redirect($origUri->toString(), 301);
}

if (substr($origUri->getPath(), \strlen(Uri::base(true)), 11) === '/index.php/') {
// Remove leading index.php
$origUri->setPath(Uri::base(true) . substr($origUri->getPath(), \strlen(Uri::base(true)) + 10));
$this->app->redirect($origUri->toString(), 301);
}
}

/**
* Remove any trailing slash from URLs built in Joomla
*
Expand Down

0 comments on commit 8da0b7c

Please sign in to comment.