You can use template_redirect action hook to achieve that:
add_action('template_redirect', 'redirect_category_posts');
function redirect_category_posts() {
global $post;
if (is_single($post->ID) && in_category('category-name', $post)) {
$new_url = "http://category-name.mydomain.com/{$post->post_name}.html";
wp_redirect($new_url, 301);
exit;
}
}
Please note that both posts in main domain and in sub-domain must have the same slug or post_name for this code to work.
Related Posts:
- Best Way to Redirect Category to Page and Hide Category
- Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
- Preventing index.php?category_name=something from redirecting
- Add forward slash on categories url (serve one version of a url)
- Multisite blog converting categories to subdomains
- Optimal way to redirect home page to category archive?
- How to: 301 Redirect /category/ to /customname/
- How to? Redirect /category/sci-fi/ to /category/scifi/?
- Redirecting category link to first child post
- Redirect category to first available child post
- Redirect category to url with /category
- Changing permalink from /postname/ to category/postname/
- WordPress category page redirects in .htaccess not working
- Why do my categories redirect?
- WP didn’t redirect to canonical category URL
- Sub Categories Keep Redirecting to Pages With Same Slugs
- redirecting from a general category page to another specific one
- Redirect single article permalink to paginated category page
- Automatically redirect a page to a category that share the same slug
- WordPress link to post links to category (loop)
- How to display post in custom file not in archive file?
- Use blog as base for tags and categories
- Category link redirect to custom template page instead of index.php?
- Redirect depending on category chosen
- Need help with major category and permalink re-organization
- Redirect Problems with Archive Page and GET variables
- OK to redirect the root WordPress category slug?
- How to prevent WordPress from redirecting single page to prefixed category archive with same name?
- Category pages redirecting to tab on Posts page grid
- Default URL for category dropdown select option
- WordPress Category url redirects to subdomain
- Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
- Paginations in Category Page Leads to Sample Pages
- htaccess redirect for all categories converted to tags now showing 404
- Page redirecting to category (IE only)
- Category Redirection Problem
- WP REST API: filter by category 1 AND category 2
- Get only the top level categories using get_categories() without foreach loop
- Remove / Rename Uncategorized Category In WordPress
- Fixing category count
- Can a Child Category Have More than One Parent?
- Rewrite URL with category and tag combined using WP_Rewrite
- How to get the category of the post and link it to the archive (of the category)
- Categories – create a new separate set right after the default?
- I want to order categories based upon the latest post time
- Highlight current category in wp_list_categories
- How to find all posts without category and assign the “Uncategorized”?
- How to select product category while adding new product in woocommerce? [closed]
- WordPress previous_post_link exclude posts with multiple categories
- How do I count how many top level categories there are?
- Want to hide some categories from the categories meta box in the post editor for the two specific user roles
- Getting categories id for all products in cart
- Show Different Custom Menu on Different Category Pages
- Show archives by year from just one category
- Combining post categories and link categories?
- Category being displayed without base parmalink
- wordpress sub-category ( lowercase letters + non-latin characters ) = 404
- Customize WordPress widget – Product Category
- Display categories with child category separated by letter
- Refine/Filter the Search Results by Category
- Have multiple category queries from the URL been fixed yet?
- Why is it so hard to show the URL of the current category and how to do it?
- Where and when does WordPress invoke routes
- How to get Next/previous_post_link to go through articles in top parent category
- Conditional loop for category
- Fix Warnings that shouldn’t be necessary to fix
- Notice: Trying to get property of non-object in : custom fields category
- Thumbnail for categories / taxonomies plugin?
- Pagination for query_posts();
- echo get_post_meta of all post in a category to fill up a select field
- get listed category’s id?
- Notice: Undefined variable: category_id
- I need to exclude from a query a category and a few custom taxonomies
- wp_insert_link not working
- wp_list_categories : how do I sort by ID when IDs are not sequential
- Showing different posts on category pages
- add current-cat class to single post page
- How to exclude certain categories showing in Gutenberg?
- Problem with pagination block in Gutenberg
- How to redirect to category page if page does not exists?
- Sort categories by custom field in WordPress admin
- Related to genre and category
- Get permalink to latest post in category
- How to load terms of a custom taxonomy of a product in woocommerce cart page
- restrict category to only logged in User
- Category and tag urls return 404 error
- Removin /page/2 from pagination
- get parent_category class in loop
- Group Posts by First Letter of Title categories
- Display all of a post’s categories except the current category
- Alphabetize all but one category
- How to create a category or tag available exclusively to the admins and not to the users?
- Printing direct descendants of a category with WP_Query
- wordpress category.php query for featured news item, broken pagination and repeating posts
- Echo class depending on Parent category
- Query pages by category
- List all authors by matching custom meta data on a category page
- Page view: Sorted by categories
- Anyway to hide a Category in the Categories section when adding/editing a post in WP Admin?
- Images as Categories