How to create a 301 redirect in WordPress
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-inwp_redirect
function.
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.
Read more: How to properly delete pages from your site »
Coming up next!
-
Event
SEODAY 2024
November 07, 2024 Team Yoast is at Attending SEODAY 2024! Click through to see who will be there, what we will do, and more! See where you can find us next » -
SEO webinar
Webinar: How to start with SEO (November 6, 2024)
06 November 2024 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 »
Hello Edwin. I enjoyed reading this post. For a long time, I have been thinking about creating 301 redirects for my blog but the methods have been too complicated for me since I am a beginner blogger with no background in coding. I must say I was surprised I did not know I can do that using Yoast SEO and I just learned that I can do that from this post. I still have a few questions I wanted to ask you. I want to increase traffic to my blog. What is the best approach to do this via 301 redirects? Redirect just the homepage of the website I want to redirect or create 301 redirects for every page and blog post from the website I want to redirect to my current website? is there a maximum number of redirects from one site one can make whereby Google can penalize you? Is it a whitehat method to create 301 redirects from one website to another as a way of creating backlinks?
Hi there, Japheth. Thank you for your comment! I’m not sure whether you’re moving your domain or want to ‘use’ your current website to get more traffic to another website/your blog? But redirects are not meant as a trick to get more traffic to your blog. They are simply meant to make sure you don’t leave any dead ends when a page gets deleted.
If you are moving a domain, I would suggest redirecting the old pages to the new pages that make the most sense. So the homepage to the homepage, a blog page to a similar blog page. But like I said, redirects are not meant to be used as a ‘trick’ to move traffic around. Hope this helps :)
Thanks for the reply Camille. I used to get confused on where to redirect if I want to move my old website to a new website especially the old blog posts. Now I totally get it…. “Homepage to the new homepage, and a blog page to a similar blog page.” Thank you for clarifying.
Thanks for sharing, but option 4 works for me because i’m not tech savvy. Lol
That’s totally understandable Mandy! We don’t all need to become tech experts, right? That’s what our Yoast plugins are for ;)