0

I've changed the URL structure of my WordPress post (not page) from:

example.com/archive/post-id

to:

example.com/post-name

I'd like to redirect the old structure /post/ to the new one via .htaccess, or plugin.

3
  • 2
    Please take a look at WP_Rewrite and search the rewrite archives on this page. Show us your research and your efforts to solve this problem and where exactly you're stuck.
    – kaiser
    Jan 10, 2014 at 7:36
  • If xxxxx would be your post-name I don't know what the problem is, because my WordPress installation redirects from /archive/post-name to /post-name by itself. Could you show us more?
    – Scuba Kay
    Jan 10, 2014 at 13:12
  • @Scuba Kay the xxxxx is id number of post not post name, my problem is my post archive in google like example.com/archive/xxxxx and i am change my link like example.com/post-nam . so when any user opne my site from google its show him 401 error Jan 10, 2014 at 14:05

1 Answer 1

0

Try using wp_rewrite:

add_action('init', 'flush_rewrite_rules');

function custom_add_rewrite_rules( $wp_rewrite ) {
    $new_rules = array( 
        'archive/(\d+)' => '/?p=' . $wp_rewrite->preg_index(1)
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'custom_add_rewrite_rules');

Use the plugin activation hook for the flush_rewrite_rules, because you only want this to happen all the time while developing. In production this should only happen when activating your plugin.

For more about flush_rewrite_rules: flush_rewrite_rules @ WordPress.org

8
  • what you mean about index.php?p= its for post or page ?? my problem for post Jan 10, 2014 at 14:48
  • That's just a typo.
    – Scuba Kay
    Jan 10, 2014 at 14:56
  • It is not a good idea to fresh and generate the rewrite rules on each activation, if WP was fired, only on plugin activation is it fine.
    – bueltge
    Jan 10, 2014 at 15:05
  • That's what I meant.
    – Scuba Kay
    Jan 10, 2014 at 15:06
  • so what can i do ?? Jan 10, 2014 at 15:11

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.