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.
3 s
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;
} );