Skip to content

Commit 7671e93

Browse files
committed
Merge branch 'main' into 1.X
2 parents f0bc5ce + fe74704 commit 7671e93

File tree

7 files changed

+93
-5
lines changed

7 files changed

+93
-5
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ return [
6464
*/
6565
'default_status_code' => (int)env('REDIRECT_DEFAULT_STATUS', 301),
6666

67+
/*
68+
|--------------------------------------------------------------------------
69+
| Case sensitivity
70+
|--------------------------------------------------------------------------
71+
|
72+
| Whether to match URLs case sensitively or not.
73+
| Default to false because most URLs are not case sensitive.
74+
|
75+
*/
76+
'case-sensitive' => (bool) env('REDIRECT_CASE_SENSITIVE', false),
77+
6778
/*
6879
|--------------------------------------------------------------------------
6980
| Redirect Driver

config/redirection.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@
2727
*/
2828
'default_status_code' => (int)env('REDIRECT_DEFAULT_STATUS', 301),
2929

30+
/*
31+
|--------------------------------------------------------------------------
32+
| Case sensitivity
33+
|--------------------------------------------------------------------------
34+
|
35+
| Whether to match URLs case sensitively or not.
36+
| Default to false because most URLs are not case sensitive.
37+
|
38+
*/
39+
'case-sensitive' => (bool) env('REDIRECT_CASE_SENSITIVE', false),
40+
3041
/*
3142
|--------------------------------------------------------------------------
3243
| Redirect Driver

src/Drivers/FileRedirector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public function getRedirectFor(string $path): ?Redirect
1919
{
2020
$redirect = config(config("redirection.drivers.{$this->driver}.source"));
2121

22+
if (false === config('redirection.case-sensitive')) {
23+
$redirect = array_change_key_case($redirect, CASE_LOWER);
24+
$path = strtolower($path);
25+
}
26+
2227
if (! array_key_exists($path, $redirect)) {
2328
return null;
2429
}

src/Models/Redirection.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static function getStatuses(): array
6161
*/
6262
public function scopeWhereOldUrl(Builder $query, string $url): Builder
6363
{
64-
return $query->where('old_url', $url);
64+
return $query->where('old_url', config('redirection.case-sensitive') ? $url : strtolower($url));
6565
}
6666

6767
/**
@@ -74,7 +74,7 @@ public function scopeWhereOldUrl(Builder $query, string $url): Builder
7474
*/
7575
public function scopeWhereNewUrl(Builder $query, string $url): Builder
7676
{
77-
return $query->where('new_url', $url);
77+
return $query->where('new_url', config('redirection.case-sensitive') ? $url : strtolower($url));
7878
}
7979

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

9192
/**
@@ -96,7 +97,8 @@ public function setOldUrlAttribute(string $value): void
9697
*/
9798
public function setNewUrlAttribute(string $value): void
9899
{
99-
$this->attributes['new_url'] = trim(parse_url($value)['path'], '/');
100+
$value = trim(parse_url($value)['path'], '/');
101+
$this->attributes['new_url'] = config('redirection.case-sensitive') ? $value : strtolower($value);
100102
}
101103

102104
/**
@@ -127,7 +129,9 @@ public function syncOldRedirects(RedirectionModelContract $model, string $finalU
127129
*/
128130
public static function findValidOrNull(string $path): ?Redirection
129131
{
130-
return static::where('old_url', $path === '/' ? $path : trim($path, '/'))
132+
$path = ($path === '/' ? $path : trim($path, '/'));
133+
134+
return static::where('old_url', config('redirection.case-sensitive') ? $path : strtolower($path))
131135
->whereNotNull('new_url')
132136
->whereIn('status_code', array_keys(self::getStatuses()))
133137
->latest()

tests/Drivers/DatabaseRedirectionTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,36 @@ public function it_redirects_a_request(): void
2222
->assertRedirect('new/url');
2323
}
2424

25+
/** @test */
26+
public function it_doesnt_redirect_case_sensitive_requests(): void
27+
{
28+
$this->app['config']->set('redirection.driver', 'database');
29+
$this->app['config']->set('redirection.case-sensitive', true);
30+
31+
Redirection::create([
32+
'old_url' => 'old-url',
33+
'new_url' => 'new/url',
34+
]);
35+
36+
$this->get('old-URL')
37+
->assertNotFound();
38+
}
39+
40+
/** @test */
41+
public function it_does_redirect_case_insensitive_requests(): void
42+
{
43+
$this->app['config']->set('redirection.driver', 'database');
44+
$this->app['config']->set('redirection.case-sensitive', false);
45+
46+
Redirection::create([
47+
'old_url' => 'old-url',
48+
'new_url' => 'new/url',
49+
]);
50+
51+
$this->get('OLD-URL')
52+
->assertRedirect('new/url');
53+
}
54+
2555
/** @test */
2656
public function it_redirects_nested_requests(): void
2757
{

tests/Drivers/FileRedirectorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,30 @@ public function it_inserts_a_path_redirection_and_redirects_a_request_with_defau
1717
$this->get('old-url')->assertRedirect('new/url');
1818
}
1919

20+
/** @test */
21+
public function it_inserts_a_path_redirection_and_redirects_a_request_with_case_sensitive_matching(): void
22+
{
23+
$this->app['config']->set('redirection.driver', 'config');
24+
$this->app['config']->set('redirection.case-sensitive', true);
25+
$this->app['config']->set('redirection.urls', [
26+
'old-url' => 'new/url',
27+
]);
28+
29+
$this->get('old-URL')->assertNotFound();
30+
}
31+
32+
/** @test */
33+
public function it_inserts_a_path_redirection_and_redirects_a_request_with_case_insensitive_matching(): void
34+
{
35+
$this->app['config']->set('redirection.driver', 'config');
36+
$this->app['config']->set('redirection.case-sensitive', false);
37+
$this->app['config']->set('redirection.urls', [
38+
'old-url' => 'new/url',
39+
]);
40+
41+
$this->get('OLD-URL')->assertRedirect('new/url');
42+
}
43+
2044
/** @test */
2145
public function it_inserts_a_path_redirection_and_redirects_a_request_with_a_custom_status_code(): void
2246
{

tests/TestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ protected function defineRoutes($router): void
3939
$router->middleware(RedirectRequests::class)->get('old-url', function () {
4040
return '';
4141
});
42+
$router->middleware(RedirectRequests::class)->get('OLD-URL', function () {
43+
return '';
44+
});
4245
$router->middleware(RedirectRequests::class)->get('/new/url', function () {
4346
return '';
4447
});

0 commit comments

Comments
 (0)