Redirect Entire WordPress Website Except One Page

Banner for post with post title overlaid over photo of car near sharp turn

Redirecting or forwarding an entire WordPress website except a single page (such as the home page) to another address is actually quite easy.

If you’re here, I’m guessing you followed a number of tutorials and tried your hand at crafting the perfect .htaccess file only to be let down every single time when it inevitably didn’t work at all.

Well have no fear! Because I’ve tried them all and finally figured out the way to do it. Note that we are going to tap into some WordPress functions so this does require an active WordPress installation but it still does the trick.

Pros of this method

  1. It works! Nothing else seems to
  2. You can optionally prevent the backend from redirecting, allowing you to continue accessing everything from the site even after the redirect is setup
  3. It’s reliable
  4. Did I mention it works?

Cons

  1. Only works with WordPress and requires you to continue hosting an entire WordPress installation
  2. Perhaps not as elegant as a theoretical, functional .htaccess solution (if it exists)
  3. You’ll need access to your site files

Without further ado, I’m going to show you the code that does the trick! Note that in the below example, “9999” is the ID of the page we wish to exclude from the redirect.

A few other notes about this:

  • If you don’t want to redirect the admin section, remove ! is_admin() &&
  • Replace http://example.com with the destination you wish to redirect traffic to
  • By default this performs a 301 (permanent) redirect; if you intent for this to be temporary, change 301 to 302
  • Obviously swap out 9999 for the page ID you wish to exclude

Seems pretty simple, right? It is!

However, if you’re not a WordPress developer, you may still be a little lost. I’ll go ahead and answer your obvious questions.

Where can I find the page ID?

It’s simple! Just navigate to the page you wish to exclude in the WordPress backend as if you were going to edit it. The post ID will be in the address bar. It will look something like this:

https://pagecrafter.com/wp-admin/post.php?post=1234&action=edit

In this case, the post ID is 1234.

Screenshot of the address bar showing an example WordPress backend URL which you can use to determine the ID number of a post.

In this screenshot, you can see an example post on my website which shows us the post ID.

Where do I add the code?

Great question!

Before proceeding, I recommend you back up your entire website in case you mess something up. If you didn’t know where to put the code before reading this article, you are unlikely to be able to fix the site if something goes wrong. You have been warned.

In general, you should add it to the end of your theme’s functions.php file. Note, however, that if you aren’t using a child theme and you have auto-updates on, it will likely be overwritten at some point when your theme forces an update and you’ll have to update it again.

With that in mind, if you have the technical ability I strongly recommend either:

  1. Using a child theme and adding the code to the theme’s functions.php file
  2. Even better, creating a custom plugin and adding the code there or simply adding it to an existing custom plugin

I’m not going to go into detail about how to do either of those, but you are welcome to look around online if you’re curious. Of the two, using a child theme is considerably easier for a novice to figure out.

I hope you found this helpful! I’m not sure how anyone could mess it up but if it didn’t work for you, let me know. Or better yet, if you figure out an .htaccess solution, send me a message and I’ll include that solution here.

About Brian Johnson

Brian Johnson is a website developer and designer living in Minneapolis, Minnesota with a passion for code and WordPress. He spends his days building WordPress websites for small businesses, developing new code with the online community, and living life.

Leave a Reply

Your email address will not be published.