I have a category archive page at: http://mysite.com/news

It displays an archive of items from the category ‘news-article’

I’d like to redirect any requests for http://mysite.com/category/news-article to http://mysite.com/news (so that the former is never directly accessible).

Is there a best practice? Should I put a 301 Redirect in my .htaccess file (or use a plugin to do the same)?

Or should I use wp_safe_redirect? If yes, which action hook should I use? As in:

add_action( 'WHICH_ACTION_HOOK??', 'adam_redirect_news' );
function adam_redirect_news () {
    if ( is_category( 'news-article' ) ) {

        wp_safe_redirect( 'http://mysite.com/news' );
        exit;
    }
}

3 s
3

I don’t know for some reason, the add_filter caused error. I used the following one:

function my_page_template_redirect()
{
    if ( is_category( 'news-articles' ) ) {
        $url = site_url( '/news' );
        wp_safe_redirect( $url, 301 );
        exit();
    }
}
add_action( 'template_redirect', 'my_page_template_redirect' );

Leave a Reply

Your email address will not be published. Required fields are marked *