Today a client of ours announced that they were purchased by another company. Inevitably with this comes website changes, and one change that was needed was to basically phase out the existing corporate website for the purchased company.
They wanted the entire website replaced with just one landing page that linked to the new owner’s website, as well as two existing division sites that were not being taken down.
Normally I would just add an item to the .htaccess file to redirect all non-home page pages to the root of the website. The problem with this, however, is that I could no longer access the admin area in WordPress. So this called for another alternative.
What I ended up doing was to create a redirect function that would fire on any front-end page.
First, I created a new page to be the home page, laid out the way the client wanted it. From there, I set the new page (that now had the page ID of 3884 – important to know later) as the static home page inside my theme (which, in this case, was a child theme we had made of the free Evolve WordPress theme).
Once that was all set, I added the following code into my child theme functions.php file:
// redirect all front-end pages to the web root
add_action(‘template_redirect’, ‘gnet_frontend_redirect’);
function gnet_frontend_redirect() {
if (!is_page(3884)) { // page ID of my new home page
wp_redirect(home_url(‘index.php?page_id=3884’), 301);
exit;
}
}
What this does is checks the page ID of the loaded page. If it’s not my new home page, it redirects to that page as a 301 redirect. Seeing that currently indexed content will no longer be needed, it doesn’t hurt me to lose it in the SERPS. The beauty of this is I can still access the administrative area, just in case. This would take more work in .htaccess.
Hopefully this might help someone else down the road.