
If you have migrated a WordPress website to a new domain, you may want to redirect posts only and leave other pages including the homepage. Most of the solutions available online are only talking about how to redirect your entire blog using a redirect plugin or redirect rules added to the htaccess file.
Redirecting single Posts in WordPress
You can redirect several WordPress posts by creating a redirect function that targets single posts, pages, or 404 error pages only.
In all the three solutions provided below, we will use the following old and new URL examples;
Old URL: https://oldwebsite.com/this-is-my-old-post/
New URL: https://newwebsite.com/this-is-my-new-post/
Solution 1: Redirect Posts Only
To redirect only single posts, add the code below to the functions.php file.
// Redirecting Posts
add_action( 'template_redirect', 'redirect_posts_to_migrated_url' );
function redirect_posts_to_migrated_url() {
if ( is_single() ) :
wp_redirect( 'https://newwebsite.com'.$_SERVER['REQUEST_URI'], 301 );
exit;
endif;
}
Explanation 1
The code above will redirect all single posts to the new website.
The if condition checks if what is being accessed is a single post before proceeding with the redirect.
wp_redirect tells your old website’s server to redirect visitors who try to access single post https://oldwebsite.com/this-is-my-old-post/ to https://newwebsite.com/this-is-my-new-post/.
Solution 2: Redirect pages only
To redirect pages only use the code below;
// Redirecting Pages
add_action( 'template_redirect', 'redirect_page_migrated_url' );
function redirect_page_migrated_url() {
if ( is_page() ) :
wp_redirect( 'https://newwebsite.com'.$_SERVER['REQUEST_URI'], 301 );
exit;
endif;
}
Explanation 2
What the code does is that it redirects old pages only to new pages. Blog posts, categories, and other files will not be redirected.
Solution 3: Redirect URLs with 404 error but not the home page
To redirect URLs with 404 error then place the code in your theme’s functions.php file;
// Redirecting 404 pages
add_action( 'template_redirect', 'redirect_404_to_migrated_url' );
function redirect_404_to_migrated_url() {
if ( is_404() ) :
wp_redirect( 'https://newwebsite.com'.$_SERVER['REQUEST_URI'], 301 );
exit;
endif;
}
Explanation 3
The code above redirects all 404 error (deleted or trashed) pages and posts to new website URLs.
Redirecting pages using 404 is the most recommend approach if you want to redirect everything in the old blog that has been trashed or deleted (pages, posts, categories, archive pages, and tags). The home page and new (posts, pages, categories, archives, and tags) will not be redirected.
Important Notes
Make sure that you are using a child theme when adding code to the functions.php file to avoid losing it when your primary theme is updated.
Remember to replace newwebsite.com with your new website domain.
301 is used to tell search engines that the URL has permanently moved. Use 302 if it is a temporary move.
$_SERVER[‘REQUEST_URI’] is used to get the absolute path or page name. For example, the absolute path for this article (https://newwebsite.com/this-is-my-new-post/) is (/this-is-my-new-post/).
In order for any of the solutions provided above to work, the old and new page name or absolute path must be the same. What can change is the root domain (https://oldwebsite.com to https://newwebsite.com).
If you are getting errors or using a subdomain or subfolder then let us know in the comment section below for a custom solution.
Leave a Reply