WordPress 301 Redirects With Code Snippets

Today I’m going to talk about WordPress 301 Redirects. I will go over the best-practiced method while using the 301 redirect directive. In addition, I will write some code snippets which you can use for yourselves.

What is a 301 redirect?

A 301 redirect directive is one of a few HTTP response status code directives you can use. Moreover, it’s one of the most powerful and commonly used today. In short, a 301 directive simply tells the client(user/ search engine bot, etc) that the URL they are trying to reach moved permanently to a new address. Resulting in an instant redirect to the new address. This is an essential tool in your arsenal.

Why should I use a 301 redirect?

Let’s take a look at this from an SEO point of view, followed by a user’s point of view.

You are running a blog with a lot of pages and posts. Naturally, sometimes things change and you might delete a post or a page. However, what most people without SEO knowledge don’t know, is, by doing soo you essentially just wipe away your SEO juice provided by the deleted content. Meaning, your posts rank on search engines. Let’s say you write a post about, for instance: “Best WordPress Plugin”. The post gets indexed by search engine bots and ultimately are indexed.

Now, if you just wipe away content without providing some sort of a response, this will result in a 404 response and a loss in the “SEO Juice” of that content. Moreover, you should know that a 404 response code is not a good thing for search engines. Especially if “something” was there before, and now it’s not.

Finally, if all of the above doesn’t affect you, think bout the potential users generated from search engine traffic(Organic Traffic) you just lose due to a 404 page instead of real content.

Now, if you didn’t know this before, and you just came to a realization after reading so far and you already did the “dirty deed”, well there is still hope. If, and I say, IF, you didn’t wipe away the Posts/Pages Trash, you can restore the post/page.

However, if you deleted a big amount of good content you might want to consider restoring a full site backup if you have one. Now, first of all, go ahead and restore that content, do it and I’ll talk about what’s next. However, for those of you whoa are in the safe zone, let’s move on.

WordPress 301 Redirects: How?

In this tutorial, I will be showing you the best method to perform a 301 redirect. However, keep in mind that there are a few ways to achieve this, so you might see different methods applied on different websites.

Our method is the way of editing the htaccesss file and providing the directives directly to our web server. This example will feature an Apache webserver. This is the best way for a few reasons, one of the main ones is simply due to the fact that it goes directly to our web server, so SPEED is a major factor here.

WordPress 301 Redirect htaccess Method

Before we begin, the htaccess file is a critical component. Make sure you backup the file before you edit it in any way.

Redirect Directive:

#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^example-post/?$  https://website.com/example-post-new-location-url/ [R=301,L,NC]

We provide the firest segment by writing the URL without the whole website prefix of “https://…”. Instead, we use what’s after the “/”. So if I had a post located at:

https://website.com/example-post – > example-post

In the second segment we provide the full website URL, much like so:

https:// website.com/example-post-new-locationn-url

Moreover, you also use 301 directives when you change a domain name. If you want to redirect specific posts from the old domain to the new one, this will look like so:

#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^example-post/?$ https://website.com/example-post-new-location-url/ [R=301,L,NC]

Now, If you want to redirect the whole old domain and whatever is under it:

RewriteRule ^(.*)$  https://newdomain.com/$1 [R=301,L]

Keep in mind, in order for the redirects to work you still must own the old domain. Moreover, you edit the htaccess of the old domain.

Before we conclude this article, I have s small bonus information for you. Another reason to use 301 redirects is to consolidate a number of the same niche content into one mega-post.

Final Words About WordPress 301 Redirects

Well, I hope you found this article useful. Always remember, don’t just delete posts and pages in your blog. Keep in might that they might have been indexed and by doing so, ultimately you will hurt your site’s SEO ranking.

Finally, with great power…comes great responsibility, Use this wisely.