how to create a 301 redirect

How to create a 301 redirect in WordPress

How to create a 301 redirect in WordPress

October 21st, 2016 – 13 Comments

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
wp_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"); 
?>

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.

Optimize your site for search & social media and keep it optimized with Yoast SEO Premium »

Yoast SEO for WordPress pluginBuy now » Info

Creating a 301 redirect 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’ »


13 Responses to How to create a 301 redirect in WordPress

  1. Mark Excell
    By Mark Excell on 4 November, 2016

    I tend to do redirects at the .htaccess level as this has the least performance impact. Also it is theme and plugin proof as by going the higher level route if you swap theme or plugins, you may find changes are lost..

  2. Koemsie Ly
    By Koemsie Ly on 4 November, 2016

    If we have problem with redirection, will our website effect ranking?

  3. Frank
    By Frank on 1 November, 2016

    In WordPress i accedently redirected a wrong page. Where can i undo this?

    The page that is redirected:
    /edit-tags.php?taxonomy=category

  4. Ivan
    By Ivan on 26 October, 2016

    Thanks for the guide. I am not really a coder so I always have a hard time with redirects. This somehow makes me understand what it means and how I can use it. Thank you.

  5. manahil
    By manahil on 25 October, 2016

    I just installed it but it does not solved my proglem, Like my domain url having www with it but i want to redirect it to without www. Does this solve this problem also ?

  6. LuciferBui
    By LuciferBui on 25 October, 2016

    How about redirect from http to https?
    I would like to do it for our website, but i don’t know how
    Thanks,

  7. Brian David
    By Brian David on 24 October, 2016

    how effective is 301?

  8. Ava
    By Ava on 23 October, 2016

    Thanks for this insightful article. 301 redirects – there should be an app that does this automatically.

  9. Edo Dijkgraaf
    By Edo Dijkgraaf on 21 October, 2016

    I am very with the results of Quick Page/Post Redirect Plugin by anadnet.

  10. Blake Imeson
    By Blake Imeson on 21 October, 2016

    I’ve been curious how the redirect manager would (or wouldn’t) work in a WPENGINE environment.

    I know the ideal in terms of performance is for redirects being added in their redirect section on WPENGINE but it has a limit of 1000. You can also add them into .htaccess there but I think that may have issues.

    I’d be curious to get a frank analysis of the pros and cons of using the redirect manager feature while on WPE.

  11. Mike
    By Mike on 21 October, 2016

    The code would not work. PHP would read the part following the double slash (//…) as a comment.

    That said, how would a PHP redirect rule like this work in WordPress? We have templates, but not single files for posts or pages. You would need a conditional statement to activate the code for certain posts or pages:

    if ( is_single( ‘id’ ) ) {
    wp_safe_redirect ( ‘http://domain.com/new-destination/’, 301 ) ; exit; }

    This would make things much more complicated than the good old .htaccess redirects.

    • Jimmy Comack
      By Jimmy Comack on 25 October, 2016

      Thank you for your response and for pointing out the flaw in my code example! Updated it so that it resembles a valid redirect.

      In regards to adding conditional statements to redirect certain post, you are entirely correct. Doing this in the code would become hard to maintain if the list of posts/pages grows rapidly. This is the exact reason why we created the Redirect Manager!

  12. Laurence Caro
    By Laurence Caro on 21 October, 2016

    Thanks for sharing Jimmy!

    I always do 301 redirects for client redesigns – it’s so important! I’ve also used Eggplant 301 Redirects and the Redirection plugin with great success.


Check out our must read articles about Analytics