Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 404 error when URLs have both ending slash and query parameters #3494

Merged
merged 6 commits into from
Apr 8, 2021
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
12 changes: 5 additions & 7 deletions src/Http/Controllers/FrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ public function __construct()
*/
public function index(Request $request)
{
$url = Site::current()->relativePath(
str_finish($request->getUri(), '/')
);

if ($url === '') {
$url = '/';
}
$url = Site::current()->relativePath($request->getUri());

if (Statamic::isAmpRequest()) {
$url = str_after($url, '/'.config('statamic.amp.route'));
Expand All @@ -45,6 +39,10 @@ public function index(Request $request)
$url = substr($url, 0, strpos($url, '?'));
}

if (Str::endsWith($url, '/') && Str::length($url) > 1) {
$url = rtrim($url, '/');
}

if ($data = Data::findByUri($url, Site::current()->handle())) {
return $data;
}
Expand Down
117 changes: 117 additions & 0 deletions tests/FrontendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,123 @@ public function page_is_displayed_with_query_string()
$this->assertEquals('<h1>The About Page</h1> <p>This is the about page.</p>', trim($response->content()));
}

/** @test */
public function page_is_displayed_with_ending_slash()
{
$this->withStandardBlueprints();
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('some_template', '<h1>{{ title }}</h1> <p>{{ content }}</p>');

$page = $this->createPage('about', [
'with' => [
'title' => 'The About Page',
'content' => 'This is the about page.',
'template' => 'some_template',
],
]);

$response = $this->get('/about/')->assertStatus(200);

$this->assertEquals('<h1>The About Page</h1> <p>This is the about page.</p>', trim($response->content()));
}

/** @test */
public function page_is_displayed_with_query_string_and_ending_slash()
{
$this->withStandardBlueprints();
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('some_template', '<h1>{{ title }}</h1> <p>{{ content }}</p>');

$page = $this->createPage('about', [
'with' => [
'title' => 'The About Page',
'content' => 'This is the about page.',
'template' => 'some_template',
],
]);

$response = $this->get('/about/?some=querystring')->assertStatus(200);

$this->assertEquals('<h1>The About Page</h1> <p>This is the about page.</p>', trim($response->content()));
}

/** @test */
public function home_page_on_second_subdirectory_based_site_is_displayed()
{
Site::setConfig(['sites' => [
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
'french' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'],
]]);

$this->createHomePagesForTwoSites();

$response = $this->get('/fr')->assertStatus(200);

$this->assertEquals('French Home', trim($response->content()));
}

/** @test */
public function home_page_on_second_subdirectory_based_site_is_displayed_with_ending_slash()
{
Site::setConfig(['sites' => [
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
'french' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'],
]]);

$this->createHomePagesForTwoSites();

$response = $this->get('/fr/')->assertStatus(200);

$this->assertEquals('French Home', trim($response->content()));
}

/** @test */
public function home_page_on_second_domain_site_is_displayed()
{
Site::setConfig(['sites' => [
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
'french' => ['url' => 'http://anotherhost.com/', 'locale' => 'fr'],
]]);

$this->createHomePagesForTwoSites();

$response = $this->get('http://anotherhost.com')->assertStatus(200);

$this->assertEquals('French Home', trim($response->content()));
}

/** @test */
public function home_page_on_second_domain_site_is_displayed_with_ending_slash()
{
Site::setConfig(['sites' => [
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
'french' => ['url' => 'http://anotherhost.com/', 'locale' => 'fr'],
]]);

$this->createHomePagesForTwoSites();

$response = $this->get('http://anotherhost.com/')->assertStatus(200);

$this->assertEquals('French Home', trim($response->content()));
}

private function createHomePagesForTwoSites()
{
$this->withStandardBlueprints();
$this->withoutExceptionHandling();
$this->withStandardFakeViews();

$c = tap(Collection::make('pages')->sites(['english', 'french'])->routes('{slug}')->structureContents(['root' => true]))->save();

EntryFactory::id('1')->locale('english')->slug('home')->collection('pages')->data(['content' => 'Home'])->create();
EntryFactory::id('2')->locale('french')->slug('french-home')->collection('pages')->data(['content' => 'French Home'])->create();

$c->structure()->in('english')->tree([['entry' => '1']])->save();
$c->structure()->in('french')->tree([['entry' => '2']])->save();
}

/** @test */
public function drafts_are_not_visible()
{
Expand Down