This website uses cookies to allow us to see how the site is used. If you continue to use this site, we assume that you are okay with this. If you want to use the sites without cookies, please see our privacy policy.

Auto-Redirect Root-Level Posts on Permalink Change

The use case here is when you’ve made the decision to switch your blog post URLs from being at the root-level (e.g. /hello-world) to using a base path (e.g. /blog/hello-world).

Typically, you’d have to carefully map out the redirects and set them up in your .htaccess file, a WordPress plugin, or in your web host’s redirect manager. Creating a generic regex solution won’t work since pages also are at the root-level.

Here is some drop-in code that will automatically detect 404’s and will redirect these root-level posts to the new URL:

add_action( 'template_redirect', function () {
	 * @var WP $wp
	global $wp;
	 * @var WP_Query $wp_query
	global $wp_query;
	if ( $wp_query->is_404() ) {
		$query = new WP_Query( array(
			'post_type'      => 'post',
			'post_status'    => 'publish',
			'name'           => $wp->request,
			'posts_per_page' => 1,
			'nopaging'       => true,
			'no_found_rows'  => true,
		) );
		if ( $query->found_posts ) {
			$redirect_url = add_query_arg( $wp->query_string, '', get_the_permalink( $query->post ) );
			wp_safe_redirect( $redirect_url, 301 );
} );


A professional WordPress developer for over a decade, Micah has worked on sites for Fortune 100 companies, has released over a dozen WordPress plugins, is a frequent speaker at WordCamps, co-organizes the WordPress Gwinnett meetup, is a co-host on the WP Square One podcast and shares his knowledge by blogging on WordPress development topics. Currently, Micah works at Bluehost as a WordPress contributor.

3 thoughts on “Auto-Redirect Root-Level Posts on Permalink Change

  1. MICAH

    Thanks for the code.

    I’m a writer with a few blogs and most of the code-related things that happen in the bowels of WordPress might as well be magic to me.

    So, why would I want to switch my blog post URLs from being at the root-level to using a base path?


  2. This is a really good idea, and I bet I’ll end up using it some time. I think what would make this *really* great is if you were to write an .htaccess redirect (or at least log it for later use) on each successful hit. That way, each 404ed post would need to be queried once and then would run a much faster redirect every time thereafter. Great idea, though. Thanks!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.