Need to create a 301 redirect in your WordPress site? You’ve come to the right place! We’ll show you how to set up 301 redirects using 4 different methods. Not sure if you need to use a redirect, or whether a 301 redirect is the right one to choose? No worries, we’ll explain that too.
Redirects in a nutshell
The name ‘redirect’ pretty much says it all: It sends visitors traveling to a specific page to an alternative one instead. Or, if there’s no alternative, an HTTP header (these are similar to redirects) can make that clear to users and search engines. It’s a bit like registering a change of address when you move house. What if an old friend goes to your old home to pay you a visit? A redirect is like a note on the front door, telling your visitors where you’re living at now. Any time that you change a URL or delete a page, you should be thinking about redirects.
There are different redirects for different purposes. As this post is all about 301 redirects, let’s look at some situations where you might need to use a 301 redirect.
When should you use a 301 redirect?
A 301 redirect should be used when:
- You’ve permanently deleted a page on your site, but you have another similar page you want to send users to instead
- You’ve changed the URL of a page that was already published
- You’re moving your site to a new domain
- You’re changing your URL structure, e.g. changing from HTTP to HTTPS, or removing ‘www’ from the start of your URL
These are some of the more common reasons for using a 301 redirect, but there are other situations that require using redirects, too. And besides that, there are other redirects and HTTP headers you can use in other situations. For instance, if you permanently delete a page and there is no suitable replacement or substitute you can send users to, then a 410 redirect is what you need to use. We’ve got a whole other post where you can read more about which redirects to use in which situations.
Option 1: Create 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 its 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.
Option 2: Create 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"); ?>
Option 3: Create a 301 redirect using the built-in WordPress function
And this is how you’d do the same, but now by using WordPress’ built-in function:
wp_redirect( "http://www.my-blog.com/a-new-destination", 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.
Option 4: Create a 301 redirect the easy way with Yoast SEO
Our Yoast SEO Premium 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.
Coming up next!
- Event October 04, 2023 Team Yoast is at Attending, Organizing Yoast Contributor day 2023 - October edition! Click through to see who will be there, what we will do, and more! See where you can find us next »
- SEO webinar 03 October 2023 Learn how to start your SEO journey the right way with our free webinar. Get practical tips and answers to all your questions in the live Q&A! All Yoast SEO webinars »