Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 or >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.
Expand Down
3 changes: 2 additions & 1 deletion config/alt-redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
|
*/

'headers' => []
'headers' => [],

'disable-enhanced-multisite' => (bool) env('ALT_REDIRECT_DISABLE_ENHANCED_MULTISITE', false),
];
23 changes: 23 additions & 0 deletions src/Helpers/URISupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Statamic\Facades\Site;

class URISupport
{
Expand Down Expand Up @@ -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
);
}
}
12 changes: 12 additions & 0 deletions src/Http/Middleware/CheckForRedirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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)) {
Expand Down