How to create a 301 redirect in WordPress
By Jimmy Comack
There will be times where you want to redirect visitors to a different part of your website when they visit a particular page or post. Reasons for this can be that you renamed a post and its URL, a page was removed or you want a different page to rank.
Redirects in a nutshell
The name ‘redirect’ pretty much says it all: It sends visitors traveling to a specific page to an alternative one. But what does this 301 mean and how does it differ from a 302 redirect? Both send your users to a different page. The only subtle (yet very important) difference is that a 301 will permanently send visitors and search engines to the new destination. 302 redirects indicate that you only temporarily want visitors to be sent to a different page.
Creating a 301 redirect on the server
One of the most basic methods of adding a 301 redirect, is by editing your .htaccess file on the server. This method is only available on Apache servers. Nginx has their own way of defining redirects in the server configuration and requires extensive knowledge of system administration.
These configurations can get quite unmaintainable over time, especially if you’re an avid blogger or you’re trying to improve the SEO of your posts. On top of that, you would have to log in on your server over FTP, edit the files and re-upload them every time you add a new redirect. That’s why, generally speaking, using this method is not considered the way to go.
Creating a 301 redirect with PHP
As a WordPress developer, you have two options: Either you make a redirect by altering the headers of a file in the code -or- you make use of WordPress’ built-in
An example of plain PHP could be as follows:
<?php // MyExampleFile.php header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.my-blog.com/a-new-destination"); ?>
And this is how you’d do the same, but now by using WordPress’ built-in function:
wp_redirect( get_permalink( $url ), 301 );
If you forget to add the
301, both WordPress and PHP will both assume that it’s a
302 redirect, which isn’t always the case.
This method is a bit easier than editing files on the server, but can also become cumbersome once the amount of redirects increases.
Buy now » Info
Creating a 301 redirect with Yoast SEO
Our Yoast SEO plugin offers you a helping hand when it comes to creating these redirects. Our built-in Redirect manager assists you whenever you change the URL of a post, page or any of the taxonomies that may result in a possible 404 if you don’t properly redirect visitors.
In addition, we also offer you an interface to edit or remove these redirects at a later point in time. The plugin also tells you when you’re about to create a redirect that will result in a redirect loop. This looping is something you want to avoid at all costs.
: ‘How to properly delete pages from your site’ »