Make WordPress Core

Opened 6 weeks ago

Closed 5 weeks ago

#63123 closed defect (bug) (fixed)

Numbered Pagination Adds a Trailing / To URLs That Generates a 404 Error

Reported by: jeffr0's profile jeffr0 Owned by: audrasjb's profile audrasjb
Milestone: 6.8 Priority: normal
Severity: normal Version: trunk
Component: Permalinks Keywords: has-patch has-testing-info has-unit-tests
Focuses: Cc:

Description

In WordPress 6.8 Beta 3, if you perform a search that generates pagination, the pagination numbers add a trailing / to the URL which generates a 404 error. However, clicking the Textual pagination aka Next or Previous Page works fine. We were able to replicate this issue in the Twenty Twenty Five theme and it does not exist in WordPress 6.7.2.

I have a screencast recording of the issue that I can show folks if need be.

Attachments (1)

63123.patch (1.5 KB) - added by ruturajraval2305 6 weeks ago.

Download all attachments as: .zip

Change History (24)

#1 @jeffr0
6 weeks ago

  • Summary changed from Numbered Pagination Adds a Trailing / To URLs That Generat a 404 Error to Numbered Pagination Adds a Trailing / To URLs That Generates a 404 Error

#2 @SergeyBiryukov
6 weeks ago

  • Component changed from General to Permalinks
  • Milestone changed from Awaiting Review to 6.8

Hi there, thanks for the ticket! This appears to be related to [59966] / #61393.

#3 @audrasjb
6 weeks ago

  • Owner set to audrasjb
  • Status changed from new to assigned

Thanks for reporting this issue. Indeed this is probably related to [59966].
Self assigning for further investigation.

#4 @kawsar007
6 weeks ago

I confirmed this issue on WP 6.8 Beta 3 with Twenty Twenty-Five. Not present in 6.7.2.

Here's a quick workaround:

function remove_trailing_slash_from_pagination($link) {

if (strpos($link, 'page/') !== false) {

$link = untrailingslashit($link);

}
return $link;

}
add_filter('paginate_links', 'remove_trailing_slash_from_pagination');

#5 @im3dabasia1
6 weeks ago

Reproduction Report

Description

This report validates whether the issue can be reproduced.

Key instructions:

  1. Add at least 11 posts (as WordPress defaults to 10 posts per page)
  2. Make them easily searchable by using a common term in all posts
  3. Go to your site's homepage
  4. Use the search box to search for the common term you included in your posts
  5. This should return enough results to trigger pagination
  6. Observe the pagination behavior - At the bottom of the search results, you'll see numbered pagination (1, 2, 3, etc.)
  7. Click on page "2" or any numbered page link
  8. Observe that the URL will have a trailing slash that causes a 404 error

Environment

  • WordPress: 6.8-beta3
  • PHP: 8.1.29
  • Server: nginx/1.16.0
  • Database: mysqli (Server: 8.0.16 / Client: mysqlnd 8.1.29)
  • Browser: Chrome 134.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.1
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.2.0
    • WordPress Beta Tester 3.6.2

Actual Results

  1. ✅ The search produces the extra trailing slash which causes a 404 error.

Additional Notes

This is not present in the WordPress 6.7.2

Supplemental Artifacts

Attachment link for the bug: https://jmp.sh/DelKywdE

Last edited 6 weeks ago by im3dabasia1 (previous) (diff)

This ticket was mentioned in PR #8541 on WordPress/wordpress-develop by @im3dabasia1.


6 weeks ago
#6

  • Keywords has-patch added

Trac ticket: https://core.trac.wordpress.org/ticket/63123

Fix pagination links for search results by preventing trailing slashes from being added to search query parameters. This change preserves the functionality from changeset [59966] while preventing 404 errors specifically on search result pages.

The issue occurred because trailing slashes were being added after search query parameters (e.g., ?s=post/), causing malformed URLs. This change ensures numbered pagination works correctly on search pages while maintaining trailing slash consistency elsewhere like the categories page.

#7 @im3dabasia1
6 weeks ago

Hi @audrasjb, @kawsar007, @jeffr0,

I have raised a [PR https://github.com/WordPress/wordpress-develop/pull/8541] to fix the pagination issue specifically in search results.

When you have a moment, I would appreciate if you could review it and provide your feedback.

#8 @ruturajraval2305
6 weeks ago

This solution ensures that no trailing slash is added to URLs when a query string is present. By checking for the existence of a query string using the function, it modifies the behavior of the function. The condition combines permalink structure validation with query string detection to accurately handle URLs, preserving their intended structure.

#9 follow-up: @peterwilsoncc
6 weeks ago

Of the two approaches, I prefer @ruturajraval2305's suggestion of checking for the presence of a query string parameter rather than limiting the test to is_search().

It appears this issue is also affecting content with a query block and testing for a query string will prevent that too. It will also allow for plugins using query string parameters.

#10 @rishavdutta
5 weeks ago

  • Keywords has-testing-info added

Test Report

Tested PR: https://github.com/WordPress/wordpress-develop/pull/8541

Test Environment[Playground]

  • WordPress: 6.8-beta3
  • PHP Version: 7.4.31-dev
  • Database: 3.40.1
  • Browser: Google Chrome
  • OS: MacOS

Test Results

  • ✅ Upon navigating to second page from search results by clicking page 2 link on pagination links at the bottom of page, it redirects to the second page and not to 404 due to trailing slashes in URL.

Screen Recording: https://drive.google.com/file/d/1ukcvK2DUjqrdhRbOsYpFZJjMMxsAfISr/view?usp=sharing

#11 in reply to: ↑ 9 @im3dabasia1
5 weeks ago

Replying to peterwilsoncc:

Of the two approaches, I prefer @ruturajraval2305's suggestion of checking for the presence of a query string parameter rather than limiting the test to is_search().

Yes, that makes sense. Also, when a user intends to make a search query with a '?', the '?' would be encoded and replaced with '%3F', which wouldn't affect @ruturajraval2305 's solution.

Perhaps we could add a one-line comment to explain why this unique change has been added, which would help future contributors understand the rationale behind it.

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


5 weeks ago

#13 @shailu25
5 weeks ago

Test Report

Patch Tested: https://github.com/WordPress/wordpress-develop/pull/8541

Environment

OS: Windows
PHP: 8.0.30
WordPress: 6.8-beta3
Browser: Chrome
Theme: Twenty Twenty Five
Plugins: None activated

Actual Result

  • Issue Resolved With Patch.✅

Screencast

Before Patch: https://drive.google.com/file/d/1lEfJLS4hZXDL2miTk8XqPiAvnXOFjP6c/view
After Patch: https://drive.google.com/file/d/1T8S2c1r3hgnFFthbxWCXWqkB2lUOzfyC/view

Last edited 5 weeks ago by shailu25 (previous) (diff)

#14 @peterwilsoncc
5 weeks ago

I've added a suggestion to the PR to handle all query strings and realised the code should account for URL fragments too.

In a follow up ticket, it would be worth considering how to handle these use cases but it is way to late in the cycle to do so for WordPress 6.8.

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


5 weeks ago

#17 @peterwilsoncc
5 weeks ago

As the release candidate is due to land within 24 hours, I've created an alternative PR with some additional changes:

  • Addition of unit tests for search/query string parameters
  • Addition of unit tests for URL fragments
  • Moved where the trailing slashes are added so it occurs before query string parameters are added back on to the links via add_query_arg()
  • Links are only modified if they do not contain either a ? or a # with the exception of root URLs, example.com/, as browsers will add them anyway

This ticket was mentioned in Slack in #core by peterwilsoncc. View the logs.


5 weeks ago

#19 @peterwilsoncc
5 weeks ago

My PR ended up breaking the format argument when passed to the function call.

It appears the original commit ignores some of the arguments that can be passed to the function, mainly base and format. To fix this ticket, it may be best to revert [59966] and take another go at the original ticket in another release.

#20 @audrasjb
5 weeks ago

Ok let's revert [59966] then

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


5 weeks ago

#22 @audrasjb
5 weeks ago

Reverted in [60088].

#23 @peterwilsoncc
5 weeks ago

  • Resolution set to fixed
  • Status changed from assigned to closed

Thanks JB, I've closed this as fixed.

Note: See TracTickets for help on using tickets.