WordPress 301 redirect

For all you developers out there building WordPress plugins or coding in the core of WordPress, this is the “proper” way of doing a 301 redirect in WordPress:

wp_redirect(get_permalink($url),301);

Easy isn’t it? Now go off and USE IT. I’m seeing way too much people who forget to add the “,301“, and stick with the default, which is a 302….

Tags:


Yoast.com runs on the Genesis Framework

Genesis theme frameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides you with the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Read our Genesis review or get Genesis now!

26 Responses

  1. GaryBy Gary on 17 April, 2007

    Joost,

    Where in the code can you change this?

    Thanks,

    Gary.

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

    In the trackback code for instance…

    • mikeBy mike on 28 April, 2009

      Joost,

      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,

      mike

  3. sanderBy sander on 18 April, 2007

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

  4. Dick RijnekeBy 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 ValkBy Joost de Valk on 20 April, 2007

    Sander: thx.

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

  6. SaptarshiBy 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 ValkBy Joost de Valk on 2 May, 2007

    Seems like a decent article Saptarshi!

  8. SaptarshiBy Saptarshi on 2 May, 2007

    Thanks Joost !

  9. SaptarshiBy Saptarshi on 27 August, 2007

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

    http://www.domain111.com/?p=23

    to this:

    http://www.domain222.com/category/updates/

    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 ValkBy Joost de Valk on 27 August, 2007

    Hi Saptarshi,

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

  11. SaptarshiBy 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. nilBy nil on 26 March, 2008

    hi,
    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. CarlBy 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?

    Thanks

  14. Joost de ValkBy 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. CarlBy 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. JeeremieBy 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.

    Thanks

  17. JeeremieBy 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. PhillipBy Phillip on 7 August, 2008

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

  19. SNVCBy SNVC on 17 August, 2008

    This is definitely a good guide. Thanks for this.

  20. JonoBy 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. richardBy 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 FernandesBy Thiago Fernandes on 16 July, 2009

    Very very nice!
    Thanks a milion!

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

    great guides , thanks a lot!!!
    Nany

Trackbacks

  1. Redirection Permanent Link – Redirect 301…

    Trong bài viết này minh sẽ giá»›i thiệu vá»›i các bạn má»™t plugin cá»±c ký có ích cho Webmaster. Đặt biệt khi đường dẫn bài viết, thể loại, tags hay archives trên site của bạn thay đổi, hoặc bạn thay đổi …