diff --git a/README.md b/README.md index 8b64f4a..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 @@ -57,6 +63,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 Enhanced Multisite 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 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 + +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"` + +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. 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)) {