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
11 changes: 11 additions & 0 deletions config/redirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
*/
'default_status_code' => (int)env('REDIRECT_DEFAULT_STATUS', 301),

/*
|--------------------------------------------------------------------------
| Case sensitivity
|--------------------------------------------------------------------------
|
| Whether to match URLs case sensitively or not.
| Default to false because most URLs are not case sensitive.
|
*/
'case-sensitive' => (bool) env('REDIRECT_CASE_SENSITIVE', false),

/*
|--------------------------------------------------------------------------
| Redirect Driver
Expand Down
5 changes: 5 additions & 0 deletions src/Drivers/FileRedirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public function getRedirectFor(string $path): ?Redirect
{
$redirect = config(config("redirection.drivers.{$this->driver}.source"));

if (false === config('redirection.case-sensitive')) {
$redirect = array_change_key_case($redirect, CASE_LOWER);
$path = strtolower($path);
}

if (! array_key_exists($path, $redirect)) {
return null;
}
Expand Down
14 changes: 9 additions & 5 deletions src/Models/Redirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static function getStatuses(): array
*/
public function scopeWhereOldUrl(Builder $query, string $url): Builder
{
return $query->where('old_url', $url);
return $query->where('old_url', config('redirection.case-sensitive') ? $url : strtolower($url));
}

/**
Expand All @@ -74,7 +74,7 @@ public function scopeWhereOldUrl(Builder $query, string $url): Builder
*/
public function scopeWhereNewUrl(Builder $query, string $url): Builder
{
return $query->where('new_url', $url);
return $query->where('new_url', config('redirection.case-sensitive') ? $url : strtolower($url));
}

/**
Expand All @@ -85,7 +85,8 @@ public function scopeWhereNewUrl(Builder $query, string $url): Builder
*/
public function setOldUrlAttribute(string $value): void
{
$this->attributes['old_url'] = trim(parse_url($value)['path'], '/');
$value = trim(parse_url($value)['path'], '/');
$this->attributes['old_url'] = config('redirection.case-sensitive') ? $value : strtolower($value);
}

/**
Expand All @@ -96,7 +97,8 @@ public function setOldUrlAttribute(string $value): void
*/
public function setNewUrlAttribute(string $value): void
{
$this->attributes['new_url'] = trim(parse_url($value)['path'], '/');
$value = trim(parse_url($value)['path'], '/');
$this->attributes['new_url'] = config('redirection.case-sensitive') ? $value : strtolower($value);
}

/**
Expand Down Expand Up @@ -127,7 +129,9 @@ public function syncOldRedirects(RedirectionModelContract $model, string $finalU
*/
public static function findValidOrNull(string $path): ?Redirection
{
return static::where('old_url', $path === '/' ? $path : trim($path, '/'))
$path = ($path === '/' ? $path : trim($path, '/'));

return static::where('old_url', config('redirection.case-sensitive') ? $path : strtolower($path))
->whereNotNull('new_url')
->whereIn('status_code', array_keys(self::getStatuses()))
->latest()
Expand Down
30 changes: 30 additions & 0 deletions tests/Drivers/DatabaseRedirectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,36 @@ public function it_redirects_a_request(): void
->assertRedirect('new/url');
}

/** @test */
public function it_doesnt_redirect_case_sensitive_requests(): void
{
$this->app['config']->set('redirection.driver', 'database');
$this->app['config']->set('redirection.case-sensitive', true);

Redirection::create([
'old_url' => 'old-url',
'new_url' => 'new/url',
]);

$this->get('old-URL')
->assertNotFound();
}

/** @test */
public function it_does_redirect_case_insensitive_requests(): void
{
$this->app['config']->set('redirection.driver', 'database');
$this->app['config']->set('redirection.case-sensitive', false);

Redirection::create([
'old_url' => 'old-url',
'new_url' => 'new/url',
]);

$this->get('OLD-URL')
->assertRedirect('new/url');
}

/** @test */
public function it_redirects_nested_requests(): void
{
Expand Down
24 changes: 24 additions & 0 deletions tests/Drivers/FileRedirectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ public function it_inserts_a_path_redirection_and_redirects_a_request_with_defau
$this->get('old-url')->assertRedirect('new/url');
}

/** @test */
public function it_inserts_a_path_redirection_and_redirects_a_request_with_case_sensitive_matching(): void
{
$this->app['config']->set('redirection.driver', 'config');
$this->app['config']->set('redirection.case-sensitive', true);
$this->app['config']->set('redirection.urls', [
'old-url' => 'new/url',
]);

$this->get('old-URL')->assertNotFound();
}

/** @test */
public function it_inserts_a_path_redirection_and_redirects_a_request_with_case_insensitive_matching(): void
{
$this->app['config']->set('redirection.driver', 'config');
$this->app['config']->set('redirection.case-sensitive', false);
$this->app['config']->set('redirection.urls', [
'old-url' => 'new/url',
]);

$this->get('OLD-URL')->assertRedirect('new/url');
}

/** @test */
public function it_inserts_a_path_redirection_and_redirects_a_request_with_a_custom_status_code(): void
{
Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ protected function defineRoutes($router): void
$router->middleware(RedirectRequests::class)->get('old-url', function () {
return '';
});
$router->middleware(RedirectRequests::class)->get('OLD-URL', function () {
return '';
});
$router->middleware(RedirectRequests::class)->get('/new/url', function () {
return '';
});
Expand Down