Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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)?

share|improve this question
    
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 '13 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 –  bungeshea Jan 26 '13 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 '13 at 20:23
    
You can't do this with .htaccess, as it can't know the category or post date of the post –  bungeshea Jan 26 '13 at 20:27
    
@AKTed Yoast's redirect generator only works when changing the structure to /%postname%/ –  bungeshea Jan 26 '13 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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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