Redirect Entire Website Except WordPress Admin

I get asked a lot about how to handle different types of website redirects. Usually, someone wants to redirect an entire site, or maybe just redirect a subdomain.  Other times, they want to do simple one-off redirects.

Many web servers that run WordPress use Apache, which means that .htaccess rules will work. Other web servers like Nginx don’t look at your .htaccess file at all. So the most reliable way of creating a redirect regardless of the web server is to implement it in the WordPress code itself. After all, in this scenario, you want to keep the WordPress admin available… so you obviously aren’t getting rid of the old domain or site instance.


<?php

if ( ! is_admin() ) {
    wp_redirect( 'https://www.mynewwebsite.com' . $_SERVER['REQUEST_URI'], 301 );
    exit;
}

The code snippet above will get the job done and will make sure that the path the user was trying to go to is kept so you can properly handle any further redirects on the new site.

Not a coder? Don’t worry, you can download the Simple Website Redirect plugin which will do the same thing!

Comments

  1. Hello Micah,

    The Redirect URL limits me to the main domain, no subdomains (it cuts it off). Any way around this?

    Thanks!

  2. Hi,

    I am wondering if your plugin will do the job I need. I want to redirect all pages except wp-admin / login to a *non WordPress page* in the same domain. Will that work?

  3. Nice! Is it possible to redirect all requests to just another site without regarding subfolders and post-adresses? Let’s say oldsite.com/tag/whatever to newsite.com instead of newsite.com/tag/whatever?

      1. thanks but i was talking about the plugin. couldn’t find any setting to achieve this.
        Or… the right place to put the php code. i tried somewhere in my child theme but it didn’t result in what i wanted.
        also: my wp-admin is protected by a htaccess login, does that make a problem?

        and doesn’t “is_admin()” force you to be already logged in? sorry for all those questions! 🙂

      2. I see. The plugin doesn’t have a setting for that.

        I stripped down the plugin to its essence and posted it as a Gist: https://gist.github.com/wpscholar/ecbc78f174c2ab74bae8f07f8901c1cd

        You can add any custom plugin header information you want, change the URL you want to redirect to (paths are ignored) and it won’t redirect the login or backend URLs of the old site. If your WP backend is protected with an .htaccess login, that won’t matter since we really only care about the front end redirects.

        The is_admin() function is used to see if you are in the admin (backend) of WordPress, not if the user is an admin (a common point of confusion).

        You can take that file and place it in your wp-content/mu-plugins directory for it to start working immediately (you may need to create it; see https://wpscholar.com/blog/wordpress-must-use-plugins/) or into your wp-content/plugins directory, in which case you will need to activate the plugin.

  4. This is helpful, but I think you might want to update your post.

    First, the most important question isn’t answered: in which file does this php snippet go? Do I create my own?

    Second, perhaps you might want to benefit others who might be on an Apache server on the correct .htaccess syntax for the same functionality?

    Thanks!

  5. Hey I’m hoping you can help… I moved all of my blog posts from one domain to another… but I want the pages to remain active on the old domain.

    Is there a way to modify this so that all posts would redirect to the new domain (with the path intact), but any page would not redirect?

    Unfortunately all my posts were setup as /post-name in permalinks

    Greatly appreciate your help!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.