Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Redirect only blog post links from old site to new? WordPress

Hi, I have two sites which are both running WordPress. Site #1 used to have a blog section, but I've moved the blog over to Site #2.

I'm trying to redirect the old blog post links from site #1 to site #2, but I'm having trouble getting this to work, because it's not a universal redirect: it needs to be specifically for blog posts only.

So domain1.com/contact should not redirect to domain2.com/contact, but domain1.com/2009/10/11/blog-post should redirect to domain2.com/2009/10/11/blog-post.

If there's a way to do this, I'd appreciate the help. Thanks!

Answers (5)

2011-12-04

Jerson Baguio answers:

You can do that via .htaccess make sure you back up your htaccess so you can revert if everything goes wrong.


RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain1.com$
RewriteRule (.*)$ http://www.domain2.com/$1 [R=301,L]


shoshanna comments:

Will that redirect only blog posts, or all the pages on the site? I need to redirect just the blog section.

2011-12-04

Maor Barazany answers:

Assuming that all of your posts' permalinks are with structure that begins with 4 digits of the year, you may use this .htacces rules.

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/\d{4}/
RewriteRule (.*)$ http://www.domain2.com/$1 [R=301,L]



Make sure you put these lines in the beginning of the file, <strong>before</strong> the #BEGIN WordPress section.

The code @Jerson Baguio wrote will redirect all pages on the site.


Maor Barazany comments:

@Alfonso's suggestion is not a best practice to handle this, and also, if it will do the change, it won't help you for future posts but only for already published. It may also create some other issues and not so recommended.

2011-12-04

Albert Shala answers:

I would recomend you check out this plugin [[LINK href="http://urbangiraffe.com/plugins/redirection/"]]http://urbangiraffe.com/plugins/redirection/[[/LINK]] if you are not comfortable editing an htaccess file.

2011-12-04

Alfonso Abarca Alvarez answers:

Hi !

for your problem can run the following queries in the database

just have to <strong>change the url and the prefix of the tables in the database (wp_)</strong>


update wp_posts set post_content = replace(post_content, 'newsite.com', 'oldsite.com');

update wp_posts set guid = replace(guid, 'newsite.com', 'oldsite.com');

update wp_options set option_value = replace(option_value, 'newsite.com', 'oldsite.com');

update wp_postmeta set meta_value = replace(meta_value, 'newsite.com', 'oldsite.com');




2011-12-05

Francisco Javier Carazo Gil answers:

You can try a high level redirect. Without using .htaccess to avoid problems trying to make difference between post and pages, in single.php you can do:


echo "<script>document.href.location = " . $new_url . $post->post_name . "</script>"


Maybe it's not the best way to have a good SEO, but it is a way to solve your problem.