Skip to content
Merged
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
13 changes: 12 additions & 1 deletion src/Redirects/HandleRedirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,23 @@ private function findRedirect(string $path, string $siteHandle): ?Redirect
private function findExactMatch(string $path, string $siteHandle): ?Redirect
{
return RedirectFacade::query()
->where('source', $path)
->whereIn('source', $this->sourceVariations($path))
->where('site', $siteHandle)
->where('enabled', true)
->first();
}

private function sourceVariations(string $path): array
{
if ($path === '/') {
return ['/'];
}

$withoutTrailingSlash = rtrim($path, '/');

return [$withoutTrailingSlash, $withoutTrailingSlash.'/'];
}

private function findWildcardMatch(string $path, string $siteHandle): ?Redirect
{
return RedirectFacade::query()
Expand Down
37 changes: 37 additions & 0 deletions tests/Redirects/HandleRedirectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Redirects;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Queue;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Collection;
Expand Down Expand Up @@ -50,6 +51,42 @@ public function it_redirects_with_302_response_code()
->assertStatus(302);
}

#[Test]
public function it_redirects_when_visiting_url_with_trailing_slash()
{
Facades\Redirect::make()
->id('abc')
->source('/old-url')
->destination('/new-url')
->responseCode(301)
->enabled(true)
->save();

// The test HTTP client strips trailing slashes before dispatching, so the
// request is handled directly through the kernel to preserve it.
$response = $this->app->handle(Request::create('/old-url/', 'GET'));

$this->assertEquals(301, $response->getStatusCode());
$this->assertEquals('/new-url', parse_url($response->headers->get('Location'), PHP_URL_PATH));
}

#[Test]
public function it_redirects_when_rule_has_trailing_slash_but_url_does_not()
{
Facades\Redirect::make()
->id('abc')
->source('/old-url/')
->destination('/new-url')
->responseCode(301)
->enabled(true)
->save();

$this
->get('/old-url')
->assertRedirect('/new-url')
->assertStatus(301);
}

#[Test]
public function it_does_not_redirect_to_inactive_redirect()
{
Expand Down