From 4353c17f702eb2bd7d59ae97b07a7d9bcde1183b Mon Sep 17 00:00:00 2001 From: Hossein Piri Date: Mon, 19 May 2025 00:56:16 +0330 Subject: [PATCH] skip appending page=1 in pagination URLs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit feat(pagination): omit ?page=1 and prevent dangling separators Skip adding the `page` query parameter when the requested page equals 1, returning the canonical base path instead. When no extra query parameters are present, the function now suppresses the `?` / `&` separator entirely. This prevents URLs such as `/users?` or `/users&` and avoids duplicate‑content issues for the first page. Behaviour for pages >1 and for existing custom query strings is unchanged. --- src/Illuminate/Pagination/AbstractPaginator.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Pagination/AbstractPaginator.php b/src/Illuminate/Pagination/AbstractPaginator.php index b27830f84ecc..3d060971540c 100644 --- a/src/Illuminate/Pagination/AbstractPaginator.php +++ b/src/Illuminate/Pagination/AbstractPaginator.php @@ -185,19 +185,25 @@ public function url($page) $page = 1; } + // Only add the page parameter if it's greater than 1. + // This prevents unnecessary ?page=1 in the URL and improves SEO by avoiding duplicate content. + $parameters = $page > 1 ? [$this->pageName => $page] : []; + // If we have any extra query string key / value pairs that need to be added // onto the URL, we will put them in query string form and then attach it // to the URL. This allows for extra information like sortings storage. - $parameters = [$this->pageName => $page]; - if (count($this->query) > 0) { $parameters = array_merge($this->query, $parameters); } + // Build the query string and attach it to the path + $query = Arr::query($parameters); + return $this->path() - .(str_contains($this->path(), '?') ? '&' : '?') - .Arr::query($parameters) - .$this->buildFragment(); + . ($query === '' + ? '' + : (str_contains($this->path(), '?') ? '&' : '?') . $query) + . $this->buildFragment(); } /**