That happens because WordPress reads your old post name as category name now – and it cannot find that category.
Solution: filter 404_template
and try to find the post and its permalink. Then redirect.
<?php # -*- coding: utf-8 -*-
/* Plugin Name: Redirect to category */
add_filter( '404_template', 't5_redirect_to_category' );
function t5_redirect_to_category( $template )
{
if ( ! is_404() )
return $template;
global $wp_rewrite, $wp_query;
if ( '/%category%/%postname%/' !== $wp_rewrite->permalink_structure )
return $template;
if ( ! $post = get_page_by_path( $wp_query->query['category_name'], OBJECT, 'post' ) )
return $template;
$permalink = get_permalink( $post->ID );
wp_redirect( $permalink, 301 );
exit;
}
Related Posts:
- Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
- How to get permalinks with category base working with sub-categories
- Multiple post categories – single permalink
- How to create a custom url for a category [closed]
- Add forward slash on categories url (serve one version of a url)
- How to Include the Parent and Child Category in the Permalink if the Post is Added to Both
- Exclude subcategories from the url
- WooCommerce change category url and product base
- Add “page-a” slug to category link if user visit category link from “page-a”
- Redirect category to url with /category
- Changing permalink from /postname/ to category/postname/
- Remove subcategory and category with some rules from permalink
- Sub Categories Keep Redirecting to Pages With Same Slugs
- Redirect single article permalink to paginated category page
- Hierarchical permalink structure for posts showing categories and sub-categories
- Need help with major category and permalink re-organization
- A different permalink structure for different categories
- How to remove category name for only one category?
- Category URL’s 404 after setting category base to ‘.’
- WordPress Category url redirects to subdomain
- Accessible Subcategory URL’s pages without using .php hack
- Multiple Parent Category URLs
- How to remove the term “category” from category pagination?
- Category archive by year with permalink support /category/YYYY
- Best Way to Redirect Category to Page and Hide Category
- Allow duplicate permalink slugs for posts in different categories
- Force WordPress to Show Pages Instead of Category
- How do I use the same post slug for different Categories?
- Optimal way to redirect home page to category archive?
- 404 error on subcategory pages
- How to create non-unique sub-category slugs?
- Category links suddenly started giving 404 errors
- How to display only the parent category in the permalink
- Permalinks – Different structures for different categories?
- custom permalinks for category archive ( custom base and no parent slug )
- New categories are now going to 404s
- Duplicate Category and Page URL loads Category instead of the Page
- Best way to programmatically link to multiple categories (union/intersection) [closed]
- “Reversable” and “Re-useable” Subcategories (or other taxonomic structure)
- Default category link for a custom category is a broken link
- Get canonical link
- wordpress wp_list_categories
- How to filter posts by format and category via url?
- Possible to make a subdirectory that loops through a single category?
- WordPress subcategories return 404 in custom template
- How do I get rid of “category” from my URL structure?
- WordPress category page redirects in .htaccess not working
- Why do my categories redirect?
- Categories showing page not found
- How to disable default category linkbuilding?
- The default code for “posts_nav_link” on category.php isn’t working
- Read GET parameters in APACHE Mod Rewrite rules for WordPress categories in Posts
- Have different permalinks based on category
- Show parent category in URL of subcategory archive page
- Mod_Rewrite to show only last subcategory in URL
- Hide uncategorized when no category set
- How to rewrite the post URL according to the category?
- Removing subcategories from permalinks [duplicate]
- Older entries link within category page
- menu using category and tags
- How can you change the permalink for pages?
- Permalinks settings
- Have multiple category queries from the URL been fixed yet?
- How to get Next/previous_post_link to go through articles in top parent category
- Hide the word ‘category’ from my permalinks
- WordPress link to post links to category (loop)
- How to Link to Most Recent Custom Post of Same Term
- Category url generate 404 error
- How to remove one specific category (uncategorized) from post permalinks
- Custom Permalink with child categories creating 404
- How do I retrieve the category ID (ugly permalinks) in my sub-navigation menu?
- Redirect year month day postname permalink structure to category postname structure
- url for posts for a date within a category
- Redirect Problems with Archive Page and GET variables
- Global navigation in multisite: problem with categories
- WordPress Primary Category Changes After Import, Permalink Issue
- How to use category slug with a regular page
- Allow two posts (from different categories) to have the same slug
- Subcategories randomly not working after page/post/category edit in wordpress. Fix: saving permalinks twice
- How to prevent WordPress from redirecting single page to prefixed category archive with same name?
- Is this process OK to replace URL structure?
- Can a Woocommerce product category URL contain “&”?
- Does having category name in permalinks affect SEO when having a post in multiple categories?
- Category pages redirecting to tab on Posts page grid
- How to 301 redirect from url with post id to permalink with post name (slug)?
- Subcategory Page 404ing
- Remove “category” from permalink with add_rewrite_rule
- Change the full slug for a specific category?
- When category name and title are the same … WP will select archive.php instead of single.php
- Paginations in Category Page Leads to Sample Pages
- Group Posts by First Letter of Title categories
- Change category base based on category
- Allow duplicate permalink slugs for posts in different categories
- htaccess redirect for all categories converted to tags now showing 404
- How to include first assigned category in the post URL?
- Custom permalinks structure
- Remove /category/ from category (archive) page URLs (without using a plugin)
- Category archive page has two URLs even when default theme is active with no active plugins
- Use same wordpress category base and tags as the prefix as the post permalink
- WordPress how to remove parent slug from category URLs