0

I have the following url rewrite for custom post type:

add_rewrite_rule(
    'event/([0-9]+)',
    'index.php?post_type=event&p=$matches[1]',
    'top' );

Due to a site structure change, I'll be moving this from /event/id to /workshop/event/id like below:

add_rewrite_rule(
    'workshop/event/([0-9]+)',
    'index.php?post_type=event&p=$matches[1]',
    'top' );

How do I return HTTP 301 on the original url scheme, from within a wordpress plugin?

2
  • The best way to do this would be in your .htaccess, are you sure you want to do this with a Plugin?
    – fischi
    Commented Mar 4, 2014 at 13:33
  • Well, I understand it's doable with http server, but I actually have other related changes, and some of them (involving database queries) might be easier to handle in plugins.
    – He Shiming
    Commented Mar 4, 2014 at 13:35

1 Answer 1

1

An easy way to do this is to keep both your rewrite rules active (for now).

On single-event.php you check for the URL, even before calling wp_header().

If the URL does not contain the base 'workshop', add a wp_redirect():

wp_redirect( get_permalink( get_the_ID() ) );

This way you should be all set. If you do not like the wp_redirect(), you can of course just modify the header information with PHP.

I know this is not in the Plugin itself. If you really depend on the Plugin, you can do the same method by hooking into an early action.

2
  • Thanks, but is there a better mechanism?
    – He Shiming
    Commented Mar 4, 2014 at 14:24
  • None that I know about, if you do not want to use the htaccess. But if you know one, I am keen to learn!
    – fischi
    Commented Mar 4, 2014 at 14:29

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.