Your code is working correctly but the logic of the code is wrong. You are doing this: if we are in “blog” category, redirect to “blog” category. So, if you go to blog category you will be redirected forever. You should think carefully about what /blog
url is and make the proper checking.
Anyway, when dealing with 301 redirections, I prefer to try .htaccess first. If it can be done through .htaccess, do it; you will save server resources because the redirection occurs before any PHP code is loaded.
Try this in your .htaccess:
RedirectMatch 301 ^/blog(.*)$ http://example.com/category/blog$1
The above code does this: if the url start with /blog(any-string-here)
, redirect to /category/blog(any-string-here)
with 301 HTTP status code.
If you want to catch exactly /blog
to /category/blog
:
RedirectMatch 301 ^/blog$ http://example.com/category/blog
Related Posts:
- Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
- Changing permalink from /postname/ to category/postname/
- Sub Categories Keep Redirecting to Pages With Same Slugs
- Redirect single article permalink to paginated category page
- Need help with major category and permalink re-organization
- WordPress Category url redirects to subdomain
- Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
- Using pagination with multiple loops causes it to break
- How do I hide posts in a category from all listings but still allow the posts to be viewed?
- Custom permalink structure leads to be 404 on pagination
- Should category.php and The Loop be used if the query needs to be customizable?
- Exclude the category from the WordPress loop
- get_field not displaying my custom field value
- Randomise results from a category page?
- How to get the url to tag & category base set by the user?
- Show div only if post is in specific category
- Redirecting specific category posts to subdomain
- How to show a category post to a specific registered user
- How to: 301 Redirect /category/ to /customname/
- How to Include the Parent and Child Category in the Permalink if the Post is Added to Both
- Root level category, tag, author and archive pages to work
- Display one latest post from multiple categories
- Archive pagination causing 404 with permalinks structure
- How do I get the total number of categories in a list of search results?
- WooCommerce change category url and product base
- Display products for a category, sorted by post_excerpt
- Redirect category to first available child post
- Redirect Loop to Page, Can’t Find Origin, Can’t Remove [closed]
- a-z list, categories and sub categories in loop
- Allow dot in wordpress permalinks (only for categories)
- How to loop only categories without posts (+ show category featured image with acf) [closed]
- force category base – archive pages work WITH & WITHOUT category base slug in url?
- Additional featured post on first page
- Show all posts for a specific category/author
- Add “page-a” slug to category link if user visit category link from “page-a”
- Class active for Archive link – looped out
- Counter in loop in foreach
- A single category with a specific permalink structure differing from the standard set for the rest of the site
- Order by category titles
- Inside a loop, how to change CSS class based on category name?
- Remove the parent name from permalink
- Limit get_categories to show each category once
- Automatically redirect a page to a category that share the same slug
- WordPress alphabetical A-Z custom post type post result display
- Hierarchical permalink structure for posts showing categories and sub-categories
- Category archive is displaying all posts rather than the specific category
- Use the category name instead of category slug in permalinks
- category__not_in and id values from variable
- Function to automatically rename the base category permalink
- How to display the parent category if only the child category has been chosen
- Allow user to select categories that will display in post loop
- Remove any product that is featured from regular display loop [WooCommerce]
- Two Sections on Woocommerce Catergory Page [closed]
- Getting the Most Recent Posts from Multiple Categories
- Display subcategories in dropdown
- Display category title and three latest post inside the category
- More efficient way to list posts by category [duplicate]
- Use blog as base for tags and categories
- category link not working for a specific slug, works for others
- Redirect depending on category chosen
- Limit Loop to One Category
- Filter Category Loop Dynamically
- 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
- Conditional category query breaking?
- Problem: retrieving parent category’s first post
- Category URL Management
- Category name for all posts getting assigned to a single random post
- Category post count is not correct
- Include last post date in get_categories loop
- Using custom structure prefix showing 404 for sub-categories
- How to add tags under categories
- How to get specific (grand parent) category of current post?
- Custom Loop with certain number of one category
- how tho change number of posts in loop from specific categories
- Get category base permalink
- Template category.php with page (no posts)
- Category URL’s 404 after setting category base to ‘.’
- error in specific category loop
- Wrong category in URLs accepted like the correct one
- loop in single.php of the same category
- Hide a category/posts from the homepage in wp
- Change Category Base For Custom Post Type or Posts Page
- Trying to display all posts in a category
- Accessible Subcategory URL’s pages without using .php hack
- Display Taxonomies in loop with template args
- same permalink for parent category and child category
- /blog/ permalink prefix causes category pages to 404
- Create monthly archive for parent category
- How to get multiple loop in category.php, my scripts becomes madness
- Most efficient use of custom taxonomies, categories, and pages
- Removing short versions of category archive URL
- Custom Category Page Not Working [duplicate]
- Change next_post and previous_link to navigate throught parent categories only
- Custom loop by url
- how wordpress generates permalinks as /%category%/%post%/
- Multiple Parent Category URLs
- IF have_posts contains posts from a certain category
- WordPress how to remove parent slug from category URLs