简体   繁体   English

将旧的自定义帖子 URL 重定向到新的 WordPress URL

[英]Redirect old custom Posts URL to new WordPress URL

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:简而言之,我将旧的自定义站点转换为新的 WordPress 站点,域保持不变,我使用 PHP 将数千篇带有评论的旧文章插入 WordPress 数据库中,这意味着相同的旧链接:

www.mysite.com/index.php?id=11058 www.mysite.com/index.php?id=11058

www.mysite.com/index.php?category=12 www.mysite.com/index.php?category=12

Than the new link are:比新链接是:

www.mysite.com/?p=11058 www.mysite.com/?p=11058

www.mysite.com/?cat=12 www.mysite.com/?cat=12

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:一切都做得很好,唯一的问题是我不想丢失旧的反向链接,我想使用 PHP 进行重定向,例如:

    if (isset($_GET['old_id'])) {  $id=$_GET['old_id']; $Wordpress_post_id = $id; }

How can I use this code in WordPress?如何在 WordPress 中使用此代码? and is this method better or redirect by .htaccess?这种方法更好还是通过 .htaccess 重定向? Or is there another way that is better than the two?或者有没有比这两者更好的方法?

I would do this in template_redirect :我会在template_redirect这样做:

This action hook executes just before WordPress determines which template page to load.此操作挂钩在 WordPress 确定要加载的模板页面之前执行。 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?是帖子元,您是否手动插入帖子 ID,是自定义查找表吗?

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.如果它是遗留 ID 到 PostID 的真实映射,您甚至可以使用带有简单规则的插件,例如重定向

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):既然你提到(并标记) .htaccess ,你可以在.htaccess文件的顶部这样做(在# BEGIN WordPress注释标记之前):

# 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.其中%1在这两种情况下都是对前面CondPattern中捕获的组的反向引用。 ie. IE。 the value of the id and category URL parameters respectively. idcategory URL 参数的值。

Test with 302 (temporary) redirects to avoid caching issues.使用 302(临时)重定向进行测试以避免缓存问题。

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.我对WordPress的几种方法进行了实验,分析了日志中的问题等,并实现了以下方法。

First I created a list of every page, article, etc.首先,我创建了每个页面、文章等的列表。

Then I created a .htaccess file located in the site root directory.然后我在站点根目录中创建了一个.htaccess文件。

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.我的 .htaccess 长约 600 行。 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.注意:我使用 302 进行重定向,如果您的重定向是永久性的,请考虑使用 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM