From 0bca1dcb4852902cdd8a4a746eb8b12aeb50f5cb Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Tue, 7 Nov 2017 17:50:06 -0700 Subject: [PATCH] Fix for custom_base_url issues #1736 --- CHANGELOG.md | 1 + system/src/Grav/Common/Uri.php | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3caa7068e..6f4d3d4d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Added `Medium::copy()` method to create a copy of a medium object * Added new `force_lowercase_urls` functionality on routes and slugs 1. [](#bugfix) + * Fixed several issues related to `system.custom_base_url` that were broken [#1736](https://github.com/getgrav/grav/issues/1736) * Dynamically added pages via `Pages::addPage()` were not firing `onPageProcessed()` event causing forms not to be processed * Fixed `Page::active()` and `Page::activeChild()` to work with UTF-8 characters in the URL [#1727](https://github.com/getgrav/grav/issues/1727) * Fixed typo in `modular.yaml` causing media to be ignored [#1725](https://github.com/getgrav/grav/issues/1725) diff --git a/system/src/Grav/Common/Uri.php b/system/src/Grav/Common/Uri.php index 473cf460e..c5e10cc9b 100644 --- a/system/src/Grav/Common/Uri.php +++ b/system/src/Grav/Common/Uri.php @@ -286,17 +286,26 @@ class Uri $this->base .= ':' . (string)$this->port; } - // Set some defaults - if ($grav['config']->get('system.custom_base_url')) { - $this->root_path = parse_url($grav['config']->get('system.custom_base_url'), PHP_URL_PATH); - $this->root = $grav['config']->get('system.custom_base_url'); + // Handle custom base + $custom_base = rtrim($grav['config']->get('system.custom_base_url'), '/'); + + if ($custom_base) { + $custom_parts = parse_url($custom_base); + $orig_root_path = $this->root_path; + $this->root_path = isset($custom_parts['path']) ? rtrim($custom_parts['path'], '/') : ''; + $this->root = isset($custom_parts['scheme']) ? $custom_base : $this->base . $this->root_path; + $this->uri = Utils::replaceFirstOccurrence($orig_root_path, $this->root_path, $this->uri); + + // If custom_base_url has a domain, force absolute_urls to true + if (isset($custom_parts['scheme'])) { + $config->set('system.absolute_urls', true); + } } else { $this->root = $this->base . $this->root_path; } $this->url = $this->base . $this->uri; - // get any params and remove them $uri = str_replace($this->root, '', $this->url); // remove the setup.php based base if set: @@ -306,8 +315,9 @@ class Uri } // If configured to, redirect trailing slash URI's with a 302 redirect - if ($uri !== '/' && $config->get('system.pages.redirect_trailing_slash', false) && Utils::endsWith($uri, '/')) { - $grav->redirect(str_replace($this->root, '', rtrim($uri, '/')), 302); + $redirect = Utils::replaceLastOccurrence($this->root, '', rtrim($uri, '/')); + if ($uri !== '/' && $redirect !== $this->base() && $config->get('system.pages.redirect_trailing_slash', false) && Utils::endsWith($uri, '/')) { + $grav->redirect($redirect, 302); } // process params