WordPress 301 redirect

April 17th, 2007 – 26 Comments

Create a 301 redirect with Yoast SEO


Creating a 301 redirect with PHP

If you’re a developer working with WordPress, this is the only proper way of doing a 301 redirect in WordPress:

wp_redirect( get_permalink( $url ), 301 );

It’s easy enough. But when you forget the 301, WordPress, like PHP, defaults to a 302 redirect, which is not what you want in all cases.

26 Responses to WordPress 301 redirect

  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 http://www.domain111.com to http://www.domain222.com 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} ^domain.com [NC]
    RewriteRule ^(.*)$ http://www.newdomain.com/$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 http://wordpress.org/extend/plugins/permalinks-moved-permanently/#post-2209 and ensure that the you have dumpled your old WP DB onto the new domain.

    Once you have done this, when someone types in http://www.domain.com/?p=23 he should be redirected to http://www.newdomain.com/?p=23 and then the plugin would redirect http://www.newdomain.com/?p=23 to the corresponding URL which would be in format of http://www.newdomain.com/category/postname

    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/ http://www.domain.com/category/example 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 http://www.domain.com/?p=23 to http://www.newdomain.com/category/postname 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 http:lucidcontent.com it resolved to http://www.lucidcontent.com.

    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