jsuggs
jsuggs

Reputation: 2652

Wordpress - Migrate 404s to different domain

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.

Upvotes: 0

Views: 52

Answers (1)

fubar
fubar

Reputation: 17398

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']);
    }
});

Upvotes: 1

Related Questions