yes, it is possible. add a template_redirect
hook handler:
add_filter('template_redirect', 'redirect_post_to_canonical_url');
function redirect_post_to_canonical_url(): void
{
if (!is_single()) {
// do not redirect nothing else except posts
return;
}
$canonicalLocation = get_permalink();
$requestUri = $_SERVER['REQUEST_URI'];
$currentLocation = home_url("https://wordpress.stackexchange.com/") . substr($requestUri, 1);
if ($currentLocation === $canonicalLocation) {
// prevent from infinite redirect loop
return;
}
wp_redirect($canonicalLocation, 301);
}
Related Posts:
- Use category base slug in posts’ permalink
- How to control which category will be picked for the slug of a post?
- Where is the old post permalink slug stored?
- How to automatically generate a unique random slug
- Post slugs and images cannot have same name?
- Change the Slug of Post Type post to baseurl/post/%postname%
- Change slug with custom field
- Display most recent post in category instead of archive?
- Post in Multiple Categories to stay in current category (permalink, next previous post link)
- How do I get the slug of a custom taxonomy category of a post?
- What Defines What Category A Post Picks (if in multiple)
- Permalink Settings: optional settings doesn’t apply to editor
- Bulk update post slugs through database
- Plain permalinks not working!
- Using permalinks, category slugs, and tag slugs
- How to handle broken links created by permalink/slug changes?
- Visting slug for ‘post’ post type shows 404 not archive?
- how do i add posts-page slug before posts slug in permalinks
- When changing pages to posts, how do you set up 301 redirects for the page URLs?
- When WP_Query only returns 1 post, how do I automatically redirect to that post?
- Categories Listing and Highlighting current category item
- Rewriting blog permalinks
- Remove subcategory slug from url
- Can I show category name in url for only one of my categories?
- How do you change the permalink for posts for a single category?
- Does having category name in permalinks affect SEO when having a post in multiple categories?
- How to add a ‘News’ section to specific posts in WordPress
- My post permalinks are always the same
- Point users to a specific url directory for some posts
- Precedence of page permalinks over woocommerce product category links?
- post_name is not stored until post is published… where should I store my slug until it goes live then?
- Make assigning post to a specific category equivalent to assigning it to all categories
- How to display subcategory articles in the category view?
- 301 Redirecting posts without %postid% (just %post_name%) in permalink to ones with %post_name%_%postid%
- Show all posts in category
- How can I fixe the article title problem?
- Different permalink for posts and authors
- Getting Position of a post in a category
- Keep getting same permalink with WP_Query?
- Display only posts from a specific parent category
- Must permalinks always point to single post pages?
- How can I specify a category post on my home page
- Use template of parent category for single post sub-categories
- How to search only by post title and category?
- Phantom 302 status code when sending a POST requests on pages
- How to change the page break URL from ../post/2 to …/post/page2/
- Why is WordPress redirecting users to random posts after commenting?
- Random post category URL
- How can i sort the categories by ID
- Display posts in three columns by category
- Add ajax load more function using WP API
- Deleted category by mistake, all posts now have categorized category. Can i revert that?
- Removing rel=”nofollow” from links posted in Post Content
- Properly display posts on homepage
- Display post X of Y in category
- Exclude category from
- Relative redirects for posts, categories, products and product categories
- Custom Post Slug same as Parents Category Slug
- 301 (static page instead latest posts)
- Automatically add date to the auto generation of post slug
- How could I change my Permalink from blog to custom structure? [closed]
- Disable sticky option for specific categories
- date issue with category post retrival
- How to make multiple sections in home pulling posts category wise?
- Permalink doesn’t get displayed in Twitter button (Local WordPress problem?)
- How do I Redirect a WordPress Page?
- How to get subcategories from category slug?
- Showing categories and subcategories with posts
- Change default category when I publish a post
- Post Image not displaying in category view
- Parent category / child category posts
- Unable to differentiate between two categories under custom post type on single.php
- Exclude post category in a blog page
- WP_Query of Category Not Showing First Post
- How to make posts being uncategorized
- How to display a single(current) post from a category
- displaying the categories post
- What’s a theme that properly handles previews of different types of posts?
- how to create a sub section in posts
- How to add content above footer in posts from specific category
- Permalink Short code showing unnecessary link text inside the loop
- How to Create another Page Category like the “Post” and “Pages”
- Why do I have categories with duplicate slugs?
- Submit New Post
- Automatic Table of contents with categories and posts
- Category URL to use same string as Post URL Permalink
- Is it possible to remove actual post pages in wordpress but keep them in categories
- Need help with hiding an image within a post in a category
- Post & Category Archives URL structure
- How to get post count including nested categories
- How to filter out post from a category not its subcategory in wordpress dashboard
- How to show main category related to that post
- Display selected category on post page
- How to use in_category?
- About number of posts in selected category
- Category page shows “No Posts” but I do have two assigned
- Exclude a ‘portfolio’ custom category?
- ‘get_previous_post’ in same category returning (!empty) when no previous post in category exists
- excerpt in template for specific page
- How to get category slug link? ( include parent categories)