10

I need to redirect a whole WordPress site to a single WordPress page. A sort of maintenance mode, but the redirect has to go to a published WordPress page. Unfortunately, the maintenance page I need to show has to use other WordPress plugins.

I am not aware of any Maintenance Mode plugin which lets you do this. At most, they let you write custom HTML/CSS code.

I was thinking about a .htaccess mod_rewrite rule. However, I am very weak with mod_rewrite.

First, I disabled canonical redirects.

Then, I tried to use:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index.php?page_id=813$
RewriteRule ^(.*)$ /index.php?page_id=813 [R=307,L]

However, these rules generate redirect loops. page_id=813 is the ID of my maintenance page, of course.

Is anybody aware of a maintenance mode plugin, which redirects to a published page?

Alternatively, can somebody help me to fix the mod_rewrite rules? Extra cheers if we can leave /wp-admin out of the redirect rules.

15

You can actually do this from inside WordPress itself, instead of needing to come up with a confusing and overengineered .htaccess fix.

We can hook into the template_redirect filter, which only fires on the front-end (not in wp-admin). We then use the is_page() function to check if we're viewing a page with the ID of 813. If not, redirect to that page using the wp_redirect() function.

add_action( 'template_redirect', function() {
    if ( is_page( 813 ) ) {
        return;
    }

    wp_redirect( esc_url_raw( home_url( 'index.php?page_id=183' ) ) );
    exit;
} );

This will work great for a maintenance mode, as the redirect is made with the 302 'temporary' HTTP header, letting bots and search engines know that your site will be up soon. However, if you're permanently moving the site, you might want to use a 301 'permanent' HTTP header for the redirect. You can do this by adding a second parameter to the wp_redirect function. Example:

add_action( 'template_redirect', function() {
    if ( is_page( 813 ) ) {
        return;
    }

    wp_redirect( esc_url_raw( home_url( 'index.php?page_id=183' ) ), 301 );
    exit;
} );
  • This works great for my purpose as well. Except adding a twist - the site is "down" for good and I want to keep WP up and running serving up the goodbye page as the home page. Is there a way to add a 301 header to this type of redirect for SEO? – Tapefreak May 24 '13 at 1:42
  • @Tapefreak You can set the HTTP header as the second parameter of the wp_redirect function. Example: wp_redirect( home_url( 'index.php?page_id=183' ), 301 ); – shea May 24 '13 at 3:30
1

I have embedded the accepted answer by @shea in a one-file plugin, and added two options: redirect only non-admin users & redirect to an arbitrary URL.

If interested, please feel free to download the plugin from Github => https://github.com/Idearia/wp-redirect-website-to-url.

Options

The plugin options are very simple; for the time being, they are hard-coded in the plugin file, but I might consider building an option page if people request it:

  • DESTINATION_URL: The full URL where to redirect users; can be a page outside the website domain.
  • DESTINATION_URL_ID: If the redirection URL is a WordPress page or post, specify here its WordPress ID.
  • USER_CAPABILITY: Users with this capability won't be redirected; leave blank to redirect everybody; default is 'manage_options' which is enabled for admin users.
  • REDIRECT_STATUS_CODE: Redirection status: 302 for temporary redirect, 301 for permanent redirect.
  • DEBUG: Whether to print debug information in debug.log.

Updated info on the Github page => https://github.com/Idearia/wp-redirect-website-to-url.

Please note that the plugin is very basic; more advanced users might consider instead one of the many maintenance plugins available on the WordPress.org plugin repository.

Let me know if you have any problem running the plugin 😊

0

I'd go for a much simpler solution. My assumption is that you only want anyone to see one page only - no need for any WP page to be visible - for any request for your domain.

So, why not create an HTML page, style it with some CSS, and put that page in another folder on your hosting platform. Make sure the page looks like you want.

Then just point your domain to that new folder. With most hosting platforms, you can specify the base folder of a domain. So point your domain to that new base folder. No need for complicated htaccess redirects, or WP plugins, or special filters to hook into WP.

Or, you could move all content out of the current domain root folder(s), and put in your simple HTML file in it's place.

May hosting places also have a 'global redirect' for any request to any page on your domain.

Either way would be a simple solution to your needs to only have one page for your entire domain.

If you really want an htaccess solution, use this one:

RewriteEngine on

RewriteRule ^(.*)$ http://www.example.com/  [R=permanent,L]

..replacing with your actual domain.Makes SEO happy also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.