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 do 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 intend 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.

9 Comments on “Redirect Entire WordPress Website Except One Page”

  1. Can I add multiple page ID’s, so that my main pages are shown, but all other pages being redirected?

    Example:

    if ( ! is_admin() && ! is_page( 9999, 9998, 9997 ))

    1. Yes, you can do that. However, the various pages must be in an array. So instead of:

      is_page( 9999, 9998, 9997 )

      You would do:

      is_page( array(9999, 9998, 9997) )

      It will return “true” if any of those are the page in question, and then the rule will not execute.

  2. Thank you for this article! This was a quick easy fix for what I needed to do and worked the first time I tried it.

  3. Hi Brian,

    Thank you for your help, it really means a lot, I have manage to solve the problem with your solution by adding my registration page ‘&& ! is_page( 9999 )’ to exclude it from the redirect.

    Sorry, just a another question regarding this code. How do you add a redirect to the last visited page after login?

    I tried adding this code, but it is redirecting to the login page because of the redirect I added on all pages:

    function admin_default_page($attributes) {
    $attributes = isset($_SERVER[‘HTTP_REFERER’]) ? $_SERVER[‘HTTP_REFERER’] : ‘/member-login’;
    return $attributes;
    }

    add_filter(‘login_redirect’, ‘admin_default_page’);

  4. Hi Brian,

    Would you perhaps know how to Redirect all pages to a custom login page except for the registration page?

    I tried implementing it, but it keeps redirect everything to my login page, so nobody can actually register for a profile.

    Here is my code:
    function admin_redirect() {
    if ( !is_user_logged_in() && !is_page( 12019 )) {
    wp_redirect( home_url(‘member-login’) );
    exit;
    }
    }
    add_action(‘get_header’, ‘admin_redirect’);

    1. Are you positive that your registration page is 12019?

      I would disable the redirect and instead have it print somewhere what the current page ID is. Then go to your registration page and see what is coming up. It may not be what you think.

  5. Would you happen to know how to exclude a directory from this? I’m using a page builder (Bricks) and when I try and edit a template, this catches it and redirects to the homepage, meaning I cant edit the template I have created in the WP admin area. The URL its redirecting is: …/template/115/?bricks=run so im hoping I can exclude the directory: /template/…

    1. You could try swapping out the conditions in the IF statement (currently ! is_admin() && ! is_page( 9999 )) with the following:

      ! is_admin() && ! is_page( 9999 ) && $_SERVER['REQUEST_URI_PATH'] != '/template/'

      That would check to see if you’re in the /template/ directory and not do the redirect if you are. It should work, anyway.

      Let me know how it goes!

Leave a Reply

Your email address will not be published. Required fields are marked *