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….



Joost,
Where in the code can you change this?
Thanks,
Gary.
In the trackback code for instance…
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
I’m not a developer, but its handy to know.
Thanks.
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.
Sander: thx.
Dick: I’d use the objection redirection plugin for that.
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.
Seems like a decent article Saptarshi!
Thanks Joost !
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 ?
Hi Saptarshi,
you could try my permalink redirect plugin, I think that’ll work…
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 :)
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?
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
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…
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?
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
By the way, I tried your plugin Joost but it did work. I am still redirected to the 404 page.
Oh my goodness, you just saved me some major time.
This is definitely a good guide. Thanks for this.
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!
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
Very very nice!
Thanks a milion!
great guides , thanks a lot!!!
Nany