In short, I converted my old custom site to a new WordPress site, the domain remains same, I used PHP to insert thousands of old articles with its comments into WordPress database maintaining the same id sequence, meaning if the old links were:
www.mysite.com/index.php?id=11058
www.mysite.com/index.php?category=12
Than the new link are:
Everything was done well, the only problem is that I don’t want to lose the old backlinks, and I want to use PHP to redirect, for example:
if (isset($_GET['old_id'])) { $id=$_GET['old_id']; $Wordpress_post_id = $id; }
How can I use this code in WordPress? and is this method better or redirect by .htaccess? Or is there another way that is better than the two?
3
Answers
I would do this in
template_redirect
:Unfortunately, the "custom stuff" is really dependent upon how you stored things. Is it post meta, did you manually insert post IDs, is it a custom lookup table?
If it is a true mapping of legacy IDs to PostIDs, you might even be able to use a plugin such as Redirection with a simple rule.
Since you mentioned (and tagged)
.htaccess
you could do it like this at the top of the.htaccess
file (before the# BEGIN WordPress
comment marker):Where
%1
is a backreference to the captured group in the preceding CondPattern in both cases. ie. the value of theid
andcategory
URL parameters respectively.Test with 302 (temporary) redirects to avoid caching issues.
I recently had to do the same thing. I reorganized a site and moved the structure from the root (and all directory structure) to the blog folder. I experimented with several methods for WordPress, analyzed logs for issues, etc., and implemented the following method.
First I created a list of every page, article, etc.
Then I created a .htaccess file located in the site root directory.
In my example below I show the redirect for one page, but with two entries (trailing slash or not). The bottom portion handles files and directors, etc.
My .htaccess is about 600 lines long. I have not noticed any performance issues with the redirects.
Note: I am using a 302 for redirects, if yours are permanent, consider using a 301.