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.1] SEF: Implementing index.php behavior #3109

Closed
jgerman-bot opened this issue Mar 1, 2024 · 0 comments · Fixed by #3112
Closed

[5.1] SEF: Implementing index.php behavior #3109

jgerman-bot opened this issue Mar 1, 2024 · 0 comments · Fixed by #3112

Comments

@jgerman-bot
Copy link

New language relevant PR in upstream repo: joomla/joomla-cms#42704 Here are the upstream changes:

Click to expand the diff!
diff --git a/administrator/language/en-GB/plg_system_sef.ini b/administrator/language/en-GB/plg_system_sef.ini
index d4be5f41bca6..654019797899 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. <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"
diff --git a/plugins/system/sef/sef.xml b/plugins/system/sef/sef.xml
index 39b87d9b4204..a0012238b554 100644
--- a/plugins/system/sef/sef.xml
+++ b/plugins/system/sef/sef.xml
@@ -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"
diff --git a/plugins/system/sef/src/Extension/Sef.php b/plugins/system/sef/src/Extension/Sef.php
index 429cf5500c55..d431f6b18e2e 100644
--- a/plugins/system/sef/src/Extension/Sef.php
+++ b/plugins/system/sef/src/Extension/Sef.php
@@ -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);
+        }
     }
 
     /**
@@ -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
      *
tecpromotion added a commit to tecpromotion/joomla that referenced this issue Mar 1, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
add translation
@tecpromotion tecpromotion linked a pull request Mar 1, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

4 participants