WordPress.org

Make WordPress Core

Opened 13 months ago

Last modified 8 months ago

#44847 new defect (bug)

Redirect old date-based permalinks on structure changes

Reported by: SergeyBiryukov Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Permalinks Keywords: needs-patch needs-unit-tests
Focuses: Cc:

Description

If someone switches from /%year%/%monthnum%/%day%/%postname%/ to /%postname%/:

  • /%year%/%postname%/ redirects to the correct URL.
  • /%year%/%monthnum%/%postname%/ shows a 404 error.
  • /%year%/%monthnum%/%day%/%postname%/ shows a 404 error.

All of these URLs should redirect to the correct one.

Change History (2)

#1 @sugavinc
13 months ago

#44849 was marked as a duplicate.

#2 @cleancoded
8 months ago

First of all change the permalink structure from Word Press Dashboard, in setting -> Permalinks change structure to post name.

Next step is redirect old URL structure to new structure from your server configuration.
For Apache you can add these redirection rules in your .htaccess file.

RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(?!page/)(.+)$ http://%{HTTP_HOST}/$4 [L,R=301]

Now the next thing is to change the links saved in your database.
As we know Word Press save data arrays as serialized string as

a:3:{s:4:"home";s:42:"https://www.example.com/2018/12/04/my-post";s:4:"link";}

It is very complicated to update these links manually.
You can use a PHP Script for search and replace word press databases. You can download it from here.

Follow the instructions and install it in a separate directory that’s a peer to wp-admin and wp-content.
I gave the directory a name like srDB_201812. Then you just go to http://example.com/srDB_201812/index.php and you’ll see a nice admin interface.

Enter your regular expression for search, what you want to replace it with, and enter your database credentials.
Perform the live run and you’re done.

Note: See TracTickets for help on using tickets.