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?
- Custom permalink structure leads to be 404 on pagination
- Posts URL structure like site.com/category/the-post-title
- 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
- Add a URL prefix to permalinks of one category of posts only
- Filtering categories in the permalink structure
- 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?
- What’s the URL for a category archive?
- Rewrite rule page url with category
- How to remove category from wordpress url?
- Preventing index.php?category_name=something from redirecting
- How to change the category in URL for posts in multiple categories?
- How to exclude uncategorized from permalink structure /%category%/%postname%/
- Multisite blog converting categories to subdomains
- Using URL parameters, list posts from category and custom taxonomy
- How to get the url to tag & category base set by the user?
- Optimal way to redirect home page to category archive?
- Redirecting specific category posts to subdomain
- 404 error on subcategory pages
- Rewrite category wordpress
- Categories’ hierarchy in URL
- How to create non-unique sub-category slugs?
- Changing the category permalink structure
- How to: 301 Redirect /category/ to /customname/
- How to? Redirect /category/sci-fi/ to /category/scifi/?
- Category links suddenly started giving 404 errors
- Best and Cleanest way to remove /category/ from category permalinks? [duplicate]
- How to display only the parent category in the permalink
- Redirecting category link to first child post
- Root level category, tag, author and archive pages to work
- Categories and Tags returning 404 on the sidebar when using ugly permalinks
- Page with the same name of a category
- Archive pagination causing 404 with permalinks structure
- Permalinks – Different structures for different categories?
- Plural Category Base Slug On Category Archive Page
- WordPress 3.4 Permalinks Redirection Problem
- custom permalinks for category archive ( custom base and no parent slug )
- Removing category base form links messes up the parent-child behavior
- Redirect category to first available child post
- New categories are now going to 404s
- Get a permalink structure of /%posttype%/%category%/%postname%
- 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
- How to set up Author archives with sub category URL
- Allow dot in wordpress permalinks (only for categories)
- Using a portfolio_category slug in wordpress URL
- Viewing category pages without the word ‘category’ in URL
- wordpress wp_list_categories
- How to get category URL with the slug?
- How to filter posts by format and category via url?
- How to Filter categories in the permalink structure
- Specifying multiple categories in URL (permalink)?
- Possible to make a subdirectory that loops through a single category?
- Changed permalink structure. Need help with redirecting old posts
- WordPress subcategories return 404 in custom template
- force category base – archive pages work WITH & WITHOUT category base slug in url?
- How get permalink for the current category or tag?
- Rewrite a category
- How do I get rid of “category” from my URL structure?
- One Specific Category To Permalink
- 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
- 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
- Redirecting thousands of posts that currently have no category in their permalink when changing permalink structure to include category