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?
- Show category name in category.php when posts assigned to multiple categories
- Permalink issue with new blog posts > getting 301 redirect
- WP posts using index.php instead of single.php
- How to increase the number posts that displays by a hierarchical taxonomy? (HTTP ERROR 500)
- how to create a category with wp_insert_post and post_category
- unable to display two post per page in wordpress front-page.php
- Get Category in save_post Hook
- Create/Set Category as Title of Post
- Add a url parameter to the current post and read it in a shortcode
- How to distinguish pages created by woocommerce?
- Exclude posts in a category on one page but show those posts on a different page
- How to create a button to filter posts (list) by meta value
- Why do I have categories with duplicate slugs?
- How to display related post from same category in single.php
- WordPress the_category(); only works with message-posts not with project posts, how do I specify project categories?
- Submit New Post
- Is there a way to save different data when USER interacts with the same POST?
- How to add a block to a category page?
- How can I set the permalink of an article / page equal to the permalink of an existing article / page?
- How to display particular categorie’s post which associated to specific user?
- Display Specific Categories posts on the home page
- Excluding category from post navigation in WordPress?
- Save All Post Permalink From A Specific Category into a .txt file
- I want to show recent post according to category of the post in single.php?
- Limit amount of categories displayed in a post
- How to make a page show posts only from specific categories without editing php files
- show image gallery in archives or category page
- Prevent Archive URLs
- Custom Post Types and Broken Permalinks
- show only one category and filter by tag
- Page with Category Returning 1
- How to redirect if is post edit or publish page?
- Automatic Table of contents with categories and posts
- Code to display random ordered posts in Categories across pages [duplicate]
- More Than 50K Categories and WordPress Admin Panel Stop Showing Categories and Posts
- Get posts from category from custom query
- Re-order Category Meta-data
- Slug collision between page, taxonomy and custom post type
- AJAX load more posts not using correct category and repeating the same few posts
- Custom permalink for wordpress page / post
- Custom post styling per category?
- Word /blog/ in slug post, problems in CPT. Solutions?
- Category posts show on local install, they do not show on live server
- get current index of post content in category page
- Permalink structure for two seperate blogs
- Category and Posts in front page
- How to show category image if no featured image is set?
- How to add category base prefix to wordpress permalink
- Allow registering user to select post category when signing up to blog
- How can I exclude only a specific sub category post from category display?
- Category URL to use same string as Post URL Permalink
- Post Category link is same with Page link
- Get 5 most recent categories
- Show custom post category for single post
- how to print total number of posts filtered by category?
- Get a list of categories that are related to posts
- How to show posts ordered by random [duplicate]
- How to Get Position of a Post from a category and tag
- Dynamically load posts based on category
- How to run select query of post with category and tags?
- Get Categories & Posts With Type Article
- Display new posts categories in separated divs
- Querying posts from current category, using a variable as array argument
- Add Categories To Custom Post