I changed the permalink structure from /%category%/%postname%/ to /%year%/%monthnum%/%postname%//

How can I redirect all old posts to the new ones (eg: via htaccess)?

  • You don't need to redirect, it's all done automatically by WordPress. As far as I understand it, WP saves your posts in the DB by their post ID's and generates the pretty permalinks on the fly based on your permalink settings.
    – akTed
    Jan 26, 2013 at 20:20
  • 1
    @AKTed While visiting the post at the new permalink will work fine, visiting the old permalink will result in a 404 error
    – shea
    Jan 26, 2013 at 20:21
  • Ahhh, wasn't thinking of that, thanks. Yoast has a .htaccess redirect generator, but I haven't tried it yet.
    – akTed
    Jan 26, 2013 at 20:23
  • You can't do this with .htaccess, as it can't know the category or post date of the post
    – shea
    Jan 26, 2013 at 20:27
  • @AKTed Yoast's redirect generator only works when changing the structure to /%postname%/
    – shea
    Jan 26, 2013 at 20:27

2 Answers 2


WordPress should handle the 301 redirects for you automatically in this case. It doesn't always, but it should here.

Long story short, WordPress has a function, redirect_guess_404_permalink which tries to find the correct post when it encounters a 404 by using the end of the 404 url to query against the wp_posts.post_name. If it finds a post, it will perform a 301 redirect. In your particular case, because your old structure ends in %postname%, this should very reliably 301 all your old paths.

  • Had a similar issue to OP. Changed permalink structure, but old links receive a 404. But I know that WP should handle this. I noticed that redirect_guess_404_permalink() guesses in a less-than strict context (i.e., post_name LIKE %s) by default. But you can change this using the strict_redirect_guess_404_permalink filter by returning true. After doing this, the old post link redirected as expected.
    – Brennan
    Aug 29, 2022 at 14:01

You can also redirect via WP. Yes, the htaccess variant is with better performance, but a small plugin is easy to use.

Use the Hook template_redirect and check for the conditional function is_404(); if true, than get the right permalink and rewrite. Also you can use the small plugin Change Permalink Helper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.