$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>htaccess - Redirect entire website to a single page|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

htaccess - Redirect entire website to a single page

matteradmin10PV0评论

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.

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.

Share Improve this question edited Dec 21, 2012 at 9:03 shea 5,6724 gold badges39 silver badges62 bronze badges asked Dec 20, 2012 at 21:30 user24742user24742
Add a comment  | 

3 Answers 3

Reset to default 16

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;
} );

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/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/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 plugin repository.

Let me know if you have any problem running the plugin

Post a comment

comment list (0)

  1. No comments so far