doulao2916
doulao2916
2017-04-28 03:16

Wordpress - 将404s迁移到不同的域

已采纳

Backstory, I've got a "php/symfony web app" that is currently hosted on www.example.com but I'm wanting to have that whole functionality be moved to app.example.com so I can spin up a wordpress site that handles all of the www traffic (its our marketing pages).

The challenge/issue is that we have links in the wild referencing www (ex www.example.com/thing/handled/by/app). So I need to find a wordpress plugin that will issue 301's for anything it doesn't know how to handle (aka wp needs to have 404 redirect to app).

So if we create a /pricing page in the wordpress site, then its available at www.example.com/pricing and handled by wordpress. But if someone goes to www.example.com/foo which is not a page wordpress is able to handle, then we have a 301 to app.example.com/foo. I'm finding some plugins that handle "known" routes but I basically need to have a wildcard redirect but for 404's only.

Let me know if additional details are necessary.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douding7189 douding7189 4年前

    Have a look at the Redirection plugin. It has a 404 log, and also supports 301 redirects using specified links or regular expression matching.

    Edit having re-read your requirements, you should be able to just add the following snippet to your theme's functions.php file.

    This will intercept the WordPress lifecycle after a query has been resolved, but before a template is rendered. If the request is not for a page in the admin panel and it is resolved to a 404, then it will redirect the request to your app instead.

    add_action('template_redirect', function () {
        if (! is_admin() && is_404()) {
            header('Location: https://app.example.com' . $_SERVER['REQUEST_URI']);
        }
    });
    
    点赞 评论 复制链接分享

相关推荐