diff --git a/src/Runtime/Http/Middleware/EnsureOnNakedDomain.php b/src/Runtime/Http/Middleware/EnsureOnNakedDomain.php index a33e90c..f3960b5 100644 --- a/src/Runtime/Http/Middleware/EnsureOnNakedDomain.php +++ b/src/Runtime/Http/Middleware/EnsureOnNakedDomain.php @@ -27,13 +27,20 @@ public function handle($request, $next) ), 301); } - if (config('vapor.redirect_to_root') === false && - strpos($request->getHost(), 'www.') === false) { - return new RedirectResponse(str_replace( - $request->getScheme().'://', - $request->getScheme().'://www.', - $request->fullUrl() - ), 301); + if (config('vapor.redirect_to_root') === false) { + $url = parse_url(config('app.url')); + + $nakedHost = preg_replace('#^www\.(.+\.)#i', '$1', $url[ + 'host' + ]); + + if ($request->getHost() === $nakedHost) { + return new RedirectResponse(str_replace( + $request->getScheme().'://', + $request->getScheme().'://www.', + $request->fullUrl() + ), 301); + } } return $next($request); diff --git a/tests/Feature/EnsureOnNakedDomainTest.php b/tests/Feature/EnsureOnNakedDomainTest.php new file mode 100644 index 0000000..fc776b5 --- /dev/null +++ b/tests/Feature/EnsureOnNakedDomainTest.php @@ -0,0 +1,130 @@ +middleware(EnsureOnNakedDomain::class); + } + + protected function getPackageProviders($app): array + { + return [ + VaporServiceProvider::class, + ]; + } + + /** + * @dataProvider useCases + */ + public function test_redirects($useCase) + { + config()->set('vapor.redirect_to_root', $useCase['redirect_to_root']); + config()->set('app.url', $useCase['app_url']); + + $response = $this->get($useCase['request_url']); + + $response->assertStatus($useCase['response_status']); + + if ($useCase['response_status'] == 301) { + $response->assertRedirect($useCase['redirected_to']); + } + } + + public function useCases() + { + return [ + [[ + 'app_url' => 'https://domain.com', + 'request_url' => 'https://domain.com', + 'redirect_to_root' => true, + 'response_status' => 200, + ]], + [[ + 'app_url' => 'https://domain.net.io', + 'request_url' => 'https://domain.net.io', + 'redirect_to_root' => true, + 'response_status' => 200, + ]], + [[ + 'app_url' => 'https://domain.com', + 'request_url' => 'https://www.domain.com', + 'redirect_to_root' => true, + 'response_status' => 301, + 'redirected_to' => 'https://domain.com', + ]], + [[ + 'app_url' => 'https://domain.net.io', + 'request_url' => 'https://www.domain.net.io', + 'redirect_to_root' => true, + 'response_status' => 301, + 'redirected_to' => 'https://domain.net.io', + ]], + [[ + 'app_url' => 'https://domain.com', + 'request_url' => 'https://sub.domain.com', + 'redirect_to_root' => true, + 'response_status' => 200, + ]], + [[ + 'app_url' => 'https://domain.net.io', + 'request_url' => 'https://sub.domain.net.io', + 'redirect_to_root' => true, + 'response_status' => 200, + ]], + + // redirect_to_root => false + [[ + 'app_url' => 'https://domain.com', + 'request_url' => 'https://www.domain.com', + 'redirect_to_root' => false, + 'response_status' => 200, + ]], + [[ + 'app_url' => 'https://domain.net.io', + 'request_url' => 'https://www.domain.net.io', + 'redirect_to_root' => false, + 'response_status' => 200, + ]], + [[ + 'app_url' => 'https://domain.com', + 'request_url' => 'https://domain.com', + 'redirect_to_root' => false, + 'response_status' => 301, + 'redirected_to' => 'https://www.domain.com', + ]], + [[ + 'app_url' => 'https://domain.net.io', + 'request_url' => 'https://domain.net.io', + 'redirect_to_root' => false, + 'response_status' => 301, + 'redirected_to' => 'https://www.domain.net.io', + ]], + [[ + 'app_url' => 'https://domain.com', + 'request_url' => 'https://sub.domain.com', + 'redirect_to_root' => false, + 'response_status' => 200, + ]], + [[ + 'app_url' => 'https://domain.net.io', + 'request_url' => 'https://sub.domain.net.io', + 'redirect_to_root' => false, + 'response_status' => 200, + ]], + ]; + } +}