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
- Remove parent category from permalink? Basically only have the child category?
- How to remove the term “category” from category pagination?
- Category archive by year with permalink support /category/YYYY
- Add a URL prefix to permalinks of one category of posts only
- Force WordPress to Show Pages Instead of Category
- How do I use the same post slug for different Categories?
- Rewrite rule page url with category
- Rewrite category wordpress
- Changing the category permalink structure
- Page with the same name of a category
- Permalinks – Different structures for different categories?
- Removing category base form links messes up the parent-child behavior
- New categories are now going to 404s
- 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
- How to set up Author archives with sub category URL
- How to filter posts by format and category via url?
- How to Filter categories in the permalink structure
- Possible to make a subdirectory that loops through a single category?
- How do I get rid of “category” from my URL structure?
- WordPress category page redirects in .htaccess not working
- Why do my categories redirect?
- Category being displayed without base parmalink
- wordpress sub-category ( lowercase letters + non-latin characters ) = 404
- Removing post category base giving Error 404
- Categories showing page not found
- 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
- Redirecting thousands of posts that currently have no category in their permalink when changing permalink structure to include category
- WP didn’t redirect to canonical category URL
- Same base for category and post
- Hide uncategorized when no category set
- WordPress URL Rewrite for dynamic and customized URL
- Older entries link within category page
- How can you change the permalink for pages?
- redirecting from a general category page to another specific one
- Get category URL knowing it’s id
- 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
- How do I add a tag slug to a category URL to filter posts?
- 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
- How do I retrieve the category ID (ugly permalinks) in my sub-navigation menu?
- url for posts for a date within a category
- Categories show 404 error on multisite installation
- Redirect Problems with Archive Page and GET variables
- Include specific category in URL automatically
- WooCommerce Product category can be shown with multiple related URLs
- How to redirect old permalinks from https://exmaple.com/category/post_id to https://exmaple.com/category/post_id.html
- Allow two posts (from different categories) to have the same slug
- How to prevent WordPress from redirecting single page to prefixed category archive with same name?
- Is this process OK to replace URL structure?
- Is it possible to set up blog categories without changing permalink structure
- Remove WP Core canonical for category pagination
- Category pages redirecting to tab on Posts page grid
- Subcategory Page 404ing
- Change the full slug for a specific category?
- Default URL for category dropdown select option
- WordPress and “pretty permalink”
- Group Posts by First Letter of Title categories
- Using categories and pages in menus
- Page Type with Category Link
- hierarchical taxonomy in permalinks? using same slug
- Changing Permalinks for Category Pages Only
- Link categories to last post
- How do I hide posts in a category from all listings but still allow the posts to be viewed?
- Create custom Perma link
- Custom permalinks structure
- Remove /category/ from category (archive) page URLs (without using a plugin)
- How to get category slug link? ( include parent categories)
- Remove “?category=” in the URL wordpress ajax filter work for category
- Category Redirection Problem
- How to remove the category from the url only for a specific category of articles on wordpress