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
- Category url generate 404 error
- How to remove one specific category (uncategorized) from post permalinks
- What Must to Display the “mysite.com/category” URL?
- Custom Permalink with child categories creating 404
- How do I retrieve the category ID (ugly permalinks) in my sub-navigation menu?
- How do I change the category permalink structure?
- How to display post in custom file not in archive file?
- Redirect year month day postname permalink structure to category postname structure
- Use blog as base for tags and categories
- category link not working for a specific slug, works for others
- Category link redirect to custom template page instead of index.php?
- Redirect depending on category chosen
- url for posts for a date within a category
- Advanced permalinks structure
- Pagination breaks on child-categories, works fine on parent-category
- Is the part “category” fixed to an URL to an archive or post?
- prefix to post permalinks without affecting category permas
- Category URL Management
- Categories show 404 error on multisite installation
- WordPress permalink issue
- Redirect Problems with Archive Page and GET variables
- Global navigation in multisite: problem with categories
- Subcategory Page URL to Display Only Posts from that Subcategory?
- Include specific category in URL automatically
- WooCommerce Product category can be shown with multiple related URLs
- WordPress Primary Category Changes After Import, Permalink Issue
- How to use category slug with a regular page
- How to redirect old permalinks from https://exmaple.com/category/post_id to https://exmaple.com/category/post_id.html
- Using custom structure prefix showing 404 for sub-categories
- Can I show category name in url for only one of my categories?
- How to add tags under categories
- Allow two posts (from different categories) to have the same slug
- OK to redirect the root WordPress category slug?
- How to set the same base url for two different taxonomies?
- Subcategories randomly not working after page/post/category edit in wordpress. Fix: saving permalinks twice
- Get multiple category names from permalink
- How to prevent WordPress from redirecting single page to prefixed category archive with same name?
- Get category base permalink
- Is this process OK to replace URL structure?
- Is it possible to set up blog categories without changing permalink structure
- Can a Woocommerce product category URL contain “&”?
- Remove WP Core canonical for category pagination
- Category page 404s on mobile but not desktop?
- Include a page’s “category” in its URL
- Does having category name in permalinks affect SEO when having a post in multiple categories?
- Wrong category in URLs accepted like the correct one
- How to add a ‘News’ section to specific posts in WordPress
- 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?
- Remove base category gives 404 error
- Custom permalink /%category%/%postname%/ produces 404 on each post
- Change Category Base For Custom Post Type or Posts Page
- Default URL for category dropdown select option
- When category name and title are the same … WP will select archive.php instead of single.php
- WordPress and “pretty permalink”
- same permalink for parent category and child category
- Get full control over custom post type url
- How to switch from custom post type URL to category URL?
- Paginations in Category Page Leads to Sample Pages
- Group Posts by First Letter of Title categories
- /blog/ permalink prefix causes category pages to 404
- Create monthly archive for parent category
- Using categories and pages in menus
- Page Type with Category Link
- hierarchical taxonomy in permalinks? using same slug
- 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
- Changing Permalinks for Category Pages Only
- Modify / Rewrite single template url structure
- Most efficient use of custom taxonomies, categories, and pages
- Removing short versions of category archive URL
- Page redirecting to category (IE only)
- Link categories to last post
- Change next_post and previous_link to navigate throught parent categories only