How to create a 301 redirect in WordPress

April 17th, 2007 – 26 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 are 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:

// MyExampleFile.php
header("HTTP/1.1 301 Moved Permanently"); 

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.

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.


26 Responses to How to create a 301 redirect in WordPress

  1. Gary
    By Gary on 17 April, 2007


    Where in the code can you change this?



  2. Joost de Valk
    By Joost de Valk on 17 April, 2007

    In the trackback code for instance…

    • mike
      By mike on 28 April, 2009


      Do you mean wp-trackback.php? I tried it here, but I still see 302s. Please enlighten me as to which files exactly I need to update.

      Thanks in advance,


  3. sander
    By sander on 18 April, 2007

    I’m not a developer, but its handy to know.

  4. Dick Rijneke
    By Dick Rijneke on 20 April, 2007

    How do I redirect pages that once existed but no longer do to a specific URL? Is that a WordPress setting or plugin as well?

    Another problem: I used my 404 page as a 301 (returned that header) and now Google Webmaster Central doesn’t verify my ownership. How can I fix this? Can’t I redirect all non-existing pages as 301.

  5. Joost de Valk
    By Joost de Valk on 20 April, 2007

    Sander: thx.

    Dick: I’d use the objection redirection plugin for that.

  6. Saptarshi
    By Saptarshi on 2 May, 2007

    Indeed a helpful post. I recently did a comprehensive post as a guide to 301 URL redirection, however, that was more about websites and not about blogging platforms like wordpress. It would be great if we have such options for 301 redirection with all blogging platforms.

  7. Joost de Valk
    By Joost de Valk on 2 May, 2007

    Seems like a decent article Saptarshi!

  8. Saptarshi
    By Saptarshi on 2 May, 2007

    Thanks Joost !

  9. Saptarshi
    By Saptarshi on 27 August, 2007

    Hi Joost, I recently came across a query where someone is using WP and wants to redirect

    to this:

    now redirecting to is pretty simple but at the page level he also wants to redirect the previous permalinks with querystrings to now static looking permalinks – which is where i a kind of stumped..

    any idea how this can be done ?

  10. Joost de Valk
    By Joost de Valk on 27 August, 2007

    Hi Saptarshi,

    you could try my permalink redirect plugin, I think that’ll work…

  11. Saptarshi
    By Saptarshi on 27 August, 2007

    Thanks Joost, even I was thinking in this line and replied this is what i sent for that:

    It is pretty simple to move from one domain to other.. all you need to write is

    Options +FollowSymLinks
    RewriteEngine on

    RewriteCond %{HTTP_HOST} ^ [NC]
    RewriteRule ^(.*)$$1 [L,R=301]

    This would redirect all your previous domain URLs to corresponding URL in new domains. But you have also changed the permalink struture which has changed the URL, so you will actually need to do some more work. Besides the above code, you can use the WP Permalink redirection plugin at and ensure that the you have dumpled your old WP DB onto the new domain.

    Once you have done this, when someone types in he should be redirected to and then the plugin would redirect to the corresponding URL which would be in format of

    I a sure there is some other way to do this eve without using that plugin, but I will have to do some more research to find that out, in case I come across any I will post it here. In the mean time you can try the method i suggested above.

    Important: always keep back up of all files when working with .htaccess and 301 redirections just to avoid any unwanted situation.

    I didn’t know about your extension.. i will post about that too :)

  12. nil
    By nil on 26 March, 2008

    i am sorry to ask but is this plugin help also to redirect from url without “www” to url with “www”,
    if yes can you help me?

  13. Carl
    By Carl on 10 April, 2008

    Hi Joost,

    I was using permalinks with /%postname%/ and then changed it to /%category%/%postname%/ in wordpress.

    I read that I should do 301 redirect in .htaccess like this…

    redirect 301 /example-post/ post/

    but i get internal 500 error. I tried a wordpress links migration plug-in… google sitemaps say “warning” too many redirects….

    Any suggestions?


  14. Joost de Valk
    By Joost de Valk on 10 April, 2008

    Hey Carl,

    which WordPress version are you running? You might want to try what happens when you do nothing… WordPress 2.3 and should be able to do the redirection automatically…

  15. Carl
    By Carl on 10 April, 2008

    Hi Joost,

    I just uploaded 2.5 …. I read where it does it automatically but when I first changed to 2.5 and went to yahoo site explorer and clicked on some of the links back to my site I got 404 page not found error. I found Deans Permalinks Migration plugin and using it now. It works okay but i get the error from Google Sitemap inside of the Google webmaster panel… kind of annoying… just dont know what to do? any ideas?

  16. Jeeremie
    By Jeeremie on 11 July, 2008

    Hi Saptarshi,

    Could you find out how to redirect in .htaccess a link like to as you wanted? I am trying to do the same thing and I can’t find any documentation about it.


  17. Jeeremie
    By Jeeremie on 11 July, 2008

    By the way, I tried your plugin Joost but it did work. I am still redirected to the 404 page.

  18. Phillip
    By Phillip on 7 August, 2008

    Oh my goodness, you just saved me some major time.

  19. SNVC
    By SNVC on 17 August, 2008

    This is definitely a good guide. Thanks for this.

  20. Jono
    By Jono on 30 March, 2009

    Hi Joost,

    I was wondering since no one has yet brought it up on this thread:

    If i want to do a canonical hostname redirect in WordPress do I still do it in the .htaccess?
    I want to redirect a non-www url to the www-url. I can get the correct code for that by just googling. What I needed to know from you since you are an expert is whether or not WP has any special .htaccess conditions??

    Thanks for the Google Analytics plugin!

  21. richard
    By richard on 22 May, 2009

    Hi and thanks for this great post. Question: I was going to do the whole 301 redirect thing. Then I learned that the WP settings page has this button… set the URL to www. So I did and it works.
    When I enter it resolved to

    So I’m done, yes? I don’t need the 301? At all? Not even for SEO purposes? Thank you very much. Richard

  22. Thiago Fernandes
    By Thiago Fernandes on 16 July, 2009

    Very very nice!
    Thanks a milion!

  23. usa used cars
    By usa used cars on 24 August, 2009

    great guides , thanks a lot!!!

Check out our must read articles about Analytics