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?
CodePudding user response:
I would do this in template_redirect
:
This action hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the content that has been queried.
add_action(
'template_redirect',
static function() {
if(!isset($_GET['old_id'])){
return;
}
// Do custom look up here, possibly get_posts()
// Once you determine where to go, use wp_safe_redirect with the appropriate code (probably 301)
// https://developer.wordpress.org/reference/functions/wp_safe_redirect/
// Also possibly use get_permalink() to find the canonical link for the object
// https://developer.wordpress.org/reference/functions/get_permalink/
}
);
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.
CodePudding user response:
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):
# Redirect "/index.php?id=<number>" to "/?p=<number>"
RewriteCond %{QUERY_STRING} ^id=(\d )
RewriteRule ^index\.php$ /?p=%1 [R=301,L]
# Redirect "/index.php?category=<number>" to "/?cat=<number>"
RewriteCond %{QUERY_STRING} ^category=(\d )
RewriteRule ^index\.php$ /?cat=%1 [R=301,L]
Where %1
is a backreference to the captured group in the preceding CondPattern in both cases. ie. the value of the id
and category
URL parameters respectively.
Test with 302 (temporary) redirects to avoid caching issues.
CodePudding user response:
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.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteRule ^aboutme$ https://www.example.com/blog/aboutme/ [L,R=302]
RewriteRule ^aboutme/$ https://www.example.com/blog/aboutme/ [L,R=302]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (. )/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>