It is common that your clients to want to change the slugs for different reasons. When the slug of the page changes, the old URL will return a 404 error. Normally, we will redirect the old URL to the new URL for SEO reasons. There are a few ways that you can do the redirect, for example, add the redirect command in .htaccess, using the wp_redirect function from WordPress or the simple way which is using the new page redirect template. In this post, I am talking about the page redirect template.
Simply, just create the new template in your theme folder. You can name the template whatever you want. The sample code will be below.
<?php /** * Template Name: Page Redirect */ header('Location: ' . site_url() . 'new-slug'); exit(); ?>
For my case, I have two pages which need to redirect to new slug. What I did, I just go to the Pages menu in the WordPress admin. Then go to those two pages, edit the page then change the template to my new page redirect template.
Next, you must flush the rewrite rule for making the new slug working. The very simple way is, go to Settings then go to Permalinks setting and just hit the Save button without any changes.
That’s it. The old slug and new slug are working correctly.