If you have recently migrated your site or changed your URLs, 301 redirects in WordPress are useful to avoid a decrease in visitors. When you migrate your website, transfer the site to HTTPS or SSL, or redesign your site in a way that changes URLs, permalinks or slug structure, your old site version with display a static “Page Not Found” message. This means that anyone clicking on an old link or shortcut to your site will not be able to see your new site. Naturally, this is not desirable, as you will lose visitors, interest and sales. A 301 redirect can permanently redirect visitors to your actual site or web page, circumventing the “Page Not Found” problem at the old address.
301 redirects in WordPress using PHP modifications
There are several way to set up 301 redirects in WordPress. If you prefer not to edit your .htaccess files or install yet another plugin, manually coding a 301 redirect into the PHP of your WordPress theme is the best course of action. The very first step is to create a child theme before you make any changes, so that your work isn’t lost when the theme updates. Placing the code in functions.php will make it apply to every page, so if you are wanting to only redirect a specific page or URL, make sure to wrap the redirect in a conditional requirement when placing the code. Here is the step by step process to add a 301 redirect to your WordPress functions.php file.
- Enter your WordPress admin control panel.
- Select Editor in the Appearance menu.
- Select Theme Functions (functions.php) in the right hand section of the window.
- At the bottom of the file, insert the WordPress PHP code for 301 redirects:
wp_redirect( ‘http://mysite.com/mynewpost’, 301 );
- Save your changes by clicking Update File.
Don’t lose out when you alter your URLs
301 redirects in WordPress do more than just redirect visitors to a new site or page. It also helps WordPress retain your SEO, backlinks and statistics, transferring it to your new page. Make sure to test that the redirect is functioning correctly by visiting your old site or page to see if you are redirected to the new version. Don’t forget to refresh your browser cache when doing this. For help setting up 301 redirects in WordPress, contact HostAfrica today.