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?
- WordPress alphabetical A-Z custom post type post result display
- Display single category name in woocommerce loop
- Show Posts from Single Category Not Working
- Is there a reason I cannot get the current category in a loop?
- 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
- Pagination on category.php and tag.php not working
- new WP_Query all post in a category inside the loop
- Trying to get variables in hacked category dropdown
- Problem with different query loops (and “main loop”) on category template page!
- How to do paging in the loop?
- How to add orderby parameter for sorting on category.php
- category__not_in and id values from variable
- WordPress link to post links to category (loop)
- How to loop custom post_type from (a) specific category(ies)?
- Do not show sub categories in the loop on archive-product.php
- How to Link to Most Recent Custom Post of Same Term
- Function to automatically rename the base category permalink
- Cannon see subcategory in permalink
- How to display the parent category if only the child category has been chosen
- Category tags with comma’s
- Redirect Changed Permalink on wordpress
- Display posts of certain categories to specific user roles
- Category url generate 404 error
- Allow user to select categories that will display in post loop
- How to remove one specific category (uncategorized) from post permalinks
- Insert HTML content in WP Query at specific point
- Custom Permalink with child categories creating 404
- How do I retrieve the category ID (ugly permalinks) in my sub-navigation menu?
- Simple way to style posts of a single category differently in the loop and in single
- How do I change the category permalink structure?
- 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
- How to display post in custom file not in archive file?
- Display category title and three latest post inside the category
- More efficient way to list posts by category [duplicate]
- Redirect year month day postname permalink structure to category postname structure
- Have posts that belong to multiple categories, exclude some categories from homepage
- Category nicename inside loop in wordpress
- Use blog as base for tags and categories
- category link not working for a specific slug, works for others
- Show multiple categories in query using redux framework variable
- Get the ID of the current post’s child category
- filter a loop base on specific category
- Problem with multiple loops in wordpress theme
- multiple values in an array for category__and does not work with WP_Query
- 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
- Limit Loop to One Category
- Display post thumbnail for specific category outside the loop
- List post only under the category, exclude child category content
- Filter Category Loop Dynamically
- 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?
- Editing Loop So It Targets Specific Tags?
- prefix to post permalinks without affecting category permas
- the_category() doesn’t working in wp_query loop
- Conditional category query breaking?
- Problem: retrieving parent category’s first post
- Category URL Management
- How to get posts using category slug in ClassiPress?
- Category sticky latest
- Categories show 404 error on multisite installation
- Posts made to category don’t show up (do show up on frontpage)
- Redirect Problems with Archive Page and GET variables
- Exclude posts without category from loop
- Subcategory Page URL to Display Only Posts from that Subcategory?
- Category name for all posts getting assigned to a single random post
- How to use category slug with a regular page
- Category post count is not correct
- Display all categories but only if they have posts in them that have a specific tag assigned
- Include last post date in get_categories loop
- Using custom structure prefix showing 404 for sub-categories
- Show the number of the post
- How to add tags under categories
- A different permalink structure for different categories
- OK to redirect the root WordPress category slug?
- How to remove category name for only one category?
- How can I order all subcategories alphabetical independent of the parent categories?
- How to get specific (grand parent) category of current post?
- 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
- Custom Loop with certain number of one category
- how tho change number of posts in loop from specific categories
- Get multiple category names from permalink
- How to prevent WordPress from redirecting single page to prefixed category archive with same name?