Opened 14 years ago
Last modified 2 weeks ago
#23074 reopened defect (bug)
Changing post's URL and then setting it back causes redirect loop (wp_old_slug_redirect)
| Reported by: | vbuterin | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Canonical | Version: | 3.5 |
| Severity: | major | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description
This morning, I changed the URL of one of my posts, but then changed my mind and set it back. When you change the URL of your post, WP automatically creates a redirect from the old page to the new page, which is good, but in this case it led to an unintended consequence: after I changed the link from A to B and then back to A, it set up a redirect from A to B and a redirect from B to A, causing anyone trying to access A or B to end up in a redirect loop between the two (or possibly it created a redirect loop from A to A; I'm not sure). It should check for such loops when you update the URL to avoid this problem.
Attachments (10)
Change History (36)
#2
@
14 years ago
- Component General → Canonical
Could not reproduce either. _wp_old_slug is properly updated to reflect the changes.
Does this still happen with all plugins deactivated and using Twenty Twelve?
#3
follow-up:
↓ 5
@
14 years ago
- Keywords close added
This sounds like a browser is caching both redirects. Little we could do here.
#4
@
13 years ago
- Keywords reporter-feedback close removed
- Resolution → invalid
- Status new → closed
I could no reproduce this. Changed url then visited old and new url both worked as expected. changed it back to same exact url as original url, visited both urls again and both worked as expected.
#5
in reply to: ↑ 3
@
13 years ago
- Resolution invalid → wontfix
Replying to nacin:
This sounds like a browser is caching both redirects. Little we could do here.
Restating that this is the issue.
#7
@
13 years ago
- Keywords has-patch 2nd-opinion added
- Resolution wontfix
- Status closed → reopened
- Summary Changing post's URL and then setting it back causes redirect loop → Changing post's URL and then setting it back causes redirect loop (wp_old_slug_redirect)
Here is how to reproduce. It may seem like a corner case, but I advocate fixing it (patch attached) because
- It is hard to debug.
- Docs and forum support for redirect loops (on custom post types) are all about calling
flush_rewrite_rules()correctly, an unrelated issue.
Steps to reproduce (with permalinks set):
- create a post 'foo'
- change the permalink to 'foobar'
- make the post Private
- change the permalink back to 'foo'
- open the post in non-logged-in browser
- result: redirect loop notice (or blank screen depending on browser)
Minimal fix attached. It's a one line SQL clause to prevent an obvious redirect loop in wp_old_slug_redirect().
Hope it's OK if I change the keywords and resolution here. I can't change the version, but it affects 3.8.1 and trunk has the same code.
#8
@
13 years ago
The patch for wp_check_for_changed_slugs() in post.php is here if you want it, and it explains the source of the problem. But I would skip it, since it's hard to test and it removes '_old_slug' values in some cases where you might want to keep them. It is better than the current code in preventing cruft, though.
@
13 years ago
Combine the minimal patch with the post patch and update the comment (to "legacy data")
#9
@
13 years ago
The new combined.2.patch does wp_check_for_changed_slugs() correctly and I think should be landed. Instead of cleaning up the data after all the early returns (current code), it cleans the data first. Compared to my previous patch, it does not delete everything when the post is Private.
#13
follow-up:
↓ 14
@
13 years ago
- Keywords needs-unit-tests 4.0-early added
- Milestone 3.9 → Future Release
I like combined.2.patch but after studying this a few times over the last few weeks, I've come to the conclusion it needs unit tests.
#14
in reply to: ↑ 13
@
12 years ago
Replying to nacin:
I like combined.2.patch but after studying this a few times over the last few weeks, I've come to the conclusion it needs unit tests.
Tried to write some unit tests (working on my unit-testing chops), but am unsure what I'm testing against. Nacin, do you have a particular logic in mind?
#15
follow-up:
↓ 23
@
12 years ago
Looking at the code in wp_check_for_changed_slugs I think the problem is that the function returns before it ever gets to add/remove slugs because the post is not publish status. I have a patch for wp_check_for_changed_slug and some unit tests.
#16
@
12 years ago
- Keywords needs-unit-tests removed
Removing the "needs-unit-tests" keywords, as I can see that Mike wrote unit tests for this ticket.
#20
@
11 years ago
- Keywords needs-refresh has-unit-tests added
Re-rolled this patch to bring it up to current TRUNK. This patch, however, causes another unit test to fail:
1) Tests_Rewrite_OldSlugRedirect::test_old_slug_redirect_attachment Failed asserting that null matches expected 'http://example.org/bar-baz/the-attachment/'. wordpress-trunk/tests/phpunit/tests/rewrite/oldSlugRedirect.php:130
#22
@
3 years ago
Found this ticket in a coding session with @poena and @afercia while testing the patch on #59866.
Specifically, comment:15 caught our attention. Moving to the milestone for further review.
#23
in reply to: ↑ 15
@
3 years ago
Replying to MikeHansenMe:
Looking at the code in wp_check_for_changed_slugs I think the problem is that the function returns before it ever gets to add/remove slugs because the post is not publish status. I have a patch for wp_check_for_changed_slug and some unit tests.
Kudos to @MikeHansenMe for discovering this. Unfortunately it's 9 years that this needs to be fixed and it's still buggy. To add some more details:
This condition in wp_check_for_changed_slugs() appears to be based on an incorrect assumption:
// We're only concerned with published, non-hierarchical objects.
if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) {
return;
}
Private posts
Private posts can be viewed by logged-in users. As such, redirects need to work for private posts as well. The problem is that a slug change isn't added/removed for private posts. No slug change detection, no redirect.
It is possible that condition dates before the introduction or private posts, not sure. Regardless it needs to be changed to take into account all cases were a post is viewable and redirects are expected to work.
Other cases
I may be missing many things here but:
- Why attachment pages are excluded? Users can change the attachment page
slugin the admin and in that case no redirect will happen. Worth reminding attachment pages can be re-enabled via the filterpre_option_wp_attachment_pages_enabled. - Why hierarchical posts (e.g. pages) are excluded? After changing a page slug, no redirect happens.
This ticket was mentioned in PR #15008 on WordPress/wordpress-develop by @abhishekfdd.
2 weeks ago
#25
- Keywords needs-refresh removed
The bug
- Publish a post with the slug
foo, then change the slug tofoobar.foois stored in_wp_old_slug. - Make the post Private.
- Change the slug back to
foo.
The post is now named foo, but foo is still stored as one of its old slugs. A logged-out request to /foo/ 404s (the post is private), and wp_old_slug_redirect() finds the post by its "old" slug foo and redirects to the post's own permalink. With date-based permalinks the same thing happens through _wp_old_date when a private post's date is changed back.
Before 5.7, a private post's permalink was its pretty URL for everyone, so a logged-out request redirected straight back to /foo/. That was a direct server-side loop. None of the early repro attempts on the ticket involved making the post private. kitchin's steps in comment:7 added that, and that's what triggers it.
Since 5.7, wp_force_plain_post_permalink() gives logged-out visitors a /?p=ID link for private posts, so the 301 now goes to /?p=ID, which 404s. Browsers cache 301s, and once the post is republished /?p=ID canonicalizes back to /foo/. So the loop now shows up only for visitors who cached that 301 before the post was republished.
Root cause
wp_check_for_changed_slugs() returns early for any status other than publish (or inherit for attachments). That early return also skips the step that removes the new slug from _wp_old_slug. wp_check_for_changed_dates() has the same guard. Neither _find_post_by_old_slug() nor _find_post_by_old_date() checks whether the post it finds is the one already using the requested slug and date.
What changed
wp_check_for_changed_slugs()andwp_check_for_changed_dates(): removing the new value from the old slugs or dates list now runs for any post status. Recording old values still follows the existing rules (published posts and inherit attachments only)._find_post_by_old_slug(): excludes posts whose currentpost_nameis the requested slug._find_post_by_old_date(): excludes posts whose current slug and date both match the request, in both the old-date query and the old-slug-plus-old-date fallback.
Why it's scoped this way
- Unconditional cleanup stops new stale meta from being written. Removing a value from the list never creates a redirect, so it's safe for any status.
- The redirect-side guard covers sites that already have bad meta from before this fix. A redirect to the post that already owns the requested slug and date can only land back on the same URL or its plain form, so it's never a useful redirect.
- The
post_nameguard is narrow. It excludes only the post already using the slug, so a different post with that old slug is still found. That's slightly better than trunk, where the query could return the self-matching post first.
Deliberately out of scope
- Recording old slugs and dates for private posts. Today, renaming a private post doesn't record its old slug. That's a separate behaviour change.
- The hierarchical post type exclusion in both functions is unchanged.
- Old slug redirects to private posts reveal the post ID to logged-out visitors even without stale meta (
/old-slug/→/?p=ID). The fix would be a readability check inwp_old_slug_redirect(). It deserves its own ticket.
Tests
Added to tests/phpunit/tests/rewrite/oldSlugRedirect.php and oldDateRedirect.php:
- The private-post repro for slugs and for dates. These check that the current value isn't kept as an old one and that the logged-out 404 doesn't redirect. They then republish the post and check that:
- the pretty URL serves it with no old-slug or canonical redirect;
- a logged-out request to
/?p=IDis canonicalized byredirect_canonical()to the pretty URL, the second half of the cached-301 loop.
- Legacy data for slugs and for dates. These seed meta equal to the current value directly, make the post private, and check that a logged-out request doesn't redirect.
All four fail on trunk. The rewrite, post, canonical and query groups pass. test_old_slug_redirect_attachment, which an earlier patch on this ticket broke, passes unchanged.
#26
@
2 weeks ago
PR: https://github.com/WordPress/wordpress-develop/pull/15008
Before 5.7, a logged-out request to the private post's URL was redirected straight back to the same pretty URL, a direct server-side loop. None of the early repro attempts here involved making the post private. kitchin's steps in comment:7 added that, and that's what triggers it. Since 5.7, wp_force_plain_post_permalink() sends the 301 to /?p=ID instead. Once the post is republished, /?p=ID canonicalizes back to the pretty URL, so it now shows up as a loop only for visitors who cached that 301 before the post was republished.
This builds on kitchin's approach in combined.2.patch and MikeHansenMe's diagnosis in comment:15, rewritten against current trunk.
The patch removes the current value from the old slugs and dates list for any post status. It also makes _find_post_by_old_slug() and _find_post_by_old_date() skip posts that already use the requested slug and date, so existing bad meta can't self-redirect. Recording old slugs for private posts and the hierarchical exclusion are unchanged. Tests cover the private-post repro, legacy stale meta, and the /?p=ID canonical redirect after republishing. test_old_slug_redirect_attachment passes.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
I couldn't reproduce this problem. I confirmed link A will forward to link B, but after changing the slug back to link A everything worked as expected.
Could you provide an example URL and the exact steps that created a redirect loop?