From e076b025d2f33602bd0ca1628fd51c984082ea4c Mon Sep 17 00:00:00 2001 From: benammi Date: Fri, 11 Apr 2025 12:27:47 +0100 Subject: [PATCH 1/3] alt-redirect.php - added 'disable-enhanced-multisite' key to flag whether the enhanced multisite support is disabled. URISupport.php - Added a function to filter the site URL off the URIs, EG "/fr" subsite would be filtered from "/fr/page" to "/page" before redirect being checked. CheckForRedirects.php - Added in optional subsite URI filtering based on config. Added in optional augmentation of the redirect URI, if enabled the redirect URL has the subsite url prepended in. Full flow EG : "/fr/old" has it's subsite URI stripped to "/old" Redirect is processed like normal, EG: "/old" -> "/new" then on the way out "/new" has the site prepended to "/fr/new" and this is used in the redirect. --- config/alt-redirect.php | 3 ++- src/Helpers/URISupport.php | 23 +++++++++++++++++++++++ src/Http/Middleware/CheckForRedirects.php | 12 ++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/config/alt-redirect.php b/config/alt-redirect.php index 6136d21..f3383fd 100644 --- a/config/alt-redirect.php +++ b/config/alt-redirect.php @@ -16,6 +16,7 @@ | */ - 'headers' => [] + 'headers' => [], + 'disable-enhanced-multisite' => (bool) env('ALT_REDIRECT_DISABLE_ENHANCED_MULTISITE', false), ]; diff --git a/src/Helpers/URISupport.php b/src/Helpers/URISupport.php index a96326a..d8b6abc 100644 --- a/src/Helpers/URISupport.php +++ b/src/Helpers/URISupport.php @@ -6,6 +6,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Str; +use Statamic\Facades\Site; class URISupport { @@ -61,4 +62,26 @@ private static function myArrQuery(array $array, $encoding_type = PHP_QUERY_RFC1 { return http_build_query($array, '', '&', $encoding_type); } + + /** + * Use the Statamic site facade to determine the current site and strip off the + * part of the URI for the site. + * + * @param string $fullUri + * @return string + */ + public static function filterSubsiteUri(string $fullUri) : string + { + $subSiteURI = Site::current()->url(); + + if ($subSiteURI == '/') { + return $fullUri; + } + + return Str::replaceFirst( + $subSiteURI, + '', + $fullUri + ); + } } diff --git a/src/Http/Middleware/CheckForRedirects.php b/src/Http/Middleware/CheckForRedirects.php index 54fe511..5a6554b 100644 --- a/src/Http/Middleware/CheckForRedirects.php +++ b/src/Http/Middleware/CheckForRedirects.php @@ -3,6 +3,7 @@ use AltDesign\AltRedirect\Helpers\URISupport; use Symfony\Component\HttpFoundation\Response; use Illuminate\Http\Request; +use Illuminate\Support\Str; use Closure; use Statamic\Facades\Site; @@ -21,6 +22,10 @@ public function handle(Request $request, Closure $next, string ...$guards): Resp { // Grab uri, make alternate / permutation $uri = URISupport::uriWithFilteredQueryStrings(); + if (! config('alt-redirect.disable-enhanced-multisite')) { + $uri = URISupport::filterSubSiteUri($uri); + } + if (str_ends_with($uri, '/')) { $permuURI = substr($uri, 0, strlen($uri) - 1); } else { @@ -65,6 +70,13 @@ public function handle(Request $request, Closure $next, string ...$guards): Resp private function redirectWithPreservedParams($to, $status) { + $siteUrl = Site::current()->url(); + if ((! Str::startsWith($to, $siteUrl)) && + (! config('alt-redirect.disable-enhanced-multisite'))) { + $to = $siteUrl . $to; + } + + $preserveKeys = []; foreach ((new Data('query-strings'))->all() as $item) { if (!($item['strip'] ?? false)) { From e5c6983cd659877f420d97b5f78b231e5fd1f14c Mon Sep 17 00:00:00 2001 From: benmAltDesign <149595159+benmAltDesign@users.noreply.github.com> Date: Fri, 11 Apr 2025 14:18:15 +0100 Subject: [PATCH 2/3] Update README.md Documentation changes for v2 --- README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.md b/README.md index 8b64f4a..c877311 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,43 @@ php artisan vendor:publish --tag=alt-redirect-config In the `headers` property, you can provide an array of headers to be passed to the `redirect` method. +#### Multisite Support + +The addon offers support for Statamic multisite setups in v2 and a legacy method to maintain pre-v2 redirect support. + +## v2 Support + +Alt Redirect v2 brings, enhanced support for subsites with differing URLs. + +| How it works | Example (Assuming "/fr" site) | +|-------------------------------------------|--------------------------------| +| URL comes in | `/fr/old` | +| The site portion is stripped out *(new)* | `/old` | +| Redirect is processed as usual | `/old` → `/new` | +| Site portion is added back in *(new 😁)* | `/fr/new` | +| Redirect happens | `return redirect("/fr/new");` | + +This means that the redirect addon will handle redirecting on any of it's subsites as long as it is flagged on the redirect. +Previously (versions v2 with enhanced subsite support disabled) you would've needed to flag the site in the redirect and include the full URI, EG: + +`"/fr/old" -> "/fr/new"` + +## Legacy >v2 Support + +This mode aims to maintain compatibility where people have an existing multisite setup and have redirects configured with the full URI including the site, EG: + +`"/fr/old" -> "/fr/new"` + +Please note : This legacy mode SHOULD NOT be used unless you have existing redirects which use the old behaviour. Fresh installs will not use legacy mode and it should not be used going forward. + +Legacy mode can be enabled with the follow env variable : + +``` +ALT_REDIRECT_DISABLE_ENHANCED_MULTISITE=true +``` + +Also note : The addon uses this env var through a config option to ensure compatibility with cached configs. + ### Query String Stripping This is a new feature we've added to remove query strings from URIs before they're processed by the redirect middleware. From c234c248c8bcf3e04e4ffc5a69ca961fc00f7a0f Mon Sep 17 00:00:00 2001 From: benmAltDesign <149595159+benmAltDesign@users.noreply.github.com> Date: Fri, 11 Apr 2025 14:22:38 +0100 Subject: [PATCH 3/3] Update README.md --- README.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c877311..0409853 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,12 @@ You can search for this addon in the `Tools > Addons` section of the Statamic co composer require alt-design/alt-redirect ``` +## Upgrading to v2 + +Your upgrade to v2 should be a smooth road, however there are some breaking changes to multisite handling that you should read if you are using a multisite. + +Dont worry though, if you need the old behaviour it can be enabled with an environment variable 🚀 + ## Basic usage ### Simple redirects @@ -61,7 +67,7 @@ In the `headers` property, you can provide an array of headers to be passed to t The addon offers support for Statamic multisite setups in v2 and a legacy method to maintain pre-v2 redirect support. -## v2 Support +#### v2 Enhanced Multisite Support Alt Redirect v2 brings, enhanced support for subsites with differing URLs. @@ -73,14 +79,14 @@ Alt Redirect v2 brings, enhanced support for subsites with differing URLs. | Site portion is added back in *(new 😁)* | `/fr/new` | | Redirect happens | `return redirect("/fr/new");` | -This means that the redirect addon will handle redirecting on any of it's subsites as long as it is flagged on the redirect. +This means that the redirect addon will handle redirecting on any of it's subsites as long as the site is flagged on the redirect. Previously (versions v2 with enhanced subsite support disabled) you would've needed to flag the site in the redirect and include the full URI, EG: `"/fr/old" -> "/fr/new"` -## Legacy >v2 Support +#### Legacy >v2 Support -This mode aims to maintain compatibility where people have an existing multisite setup and have redirects configured with the full URI including the site, EG: +This mode aims to maintain compatibility for people with an existing multisite setup who have redirects configured with the full URI including the site, EG: `"/fr/old" -> "/fr/new"`