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. 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?

  2. 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.

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.