admin管理员组

文章数量:1384394

I have multi-site wordpress, I want to redirect the main site to one of the sub-site but redirection in .htacecss gives error. Is there any way to redirect a main site to its sub-site. That looks not possible but I am not a wordpress expert so just verifying.

The redirect should be like

redirect 301 www.example/main-site www.example/main-site/sub-site

Or if there is any other way to do that?

I have multi-site wordpress, I want to redirect the main site to one of the sub-site but redirection in .htacecss gives error. Is there any way to redirect a main site to its sub-site. That looks not possible but I am not a wordpress expert so just verifying.

The redirect should be like

redirect 301 www.example/main-site www.example/main-site/sub-site

Or if there is any other way to do that?

Share Improve this question asked Sep 24, 2012 at 13:19 ToqeerToqeer 1631 silver badge7 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 10 +50

You can use the parse_request action to accomplish this. Simply enable this plugin on your primary blog. Place the following code in a .php file and upload it to your plugins directory.

/*
Plugin Name: Redirect Main Site To Sub-Site
Description: Redirect 'main-site' to 'main-site/sub-site/'
Version: 0.1
Author: WPSE
Author URI: http://wordpress.stackexchange
License: GPL2
*/

add_action('parse_request', 'redirect_to_sub_site');
function redirect_to_sub_site(){
    global $wp;

    #Sniff requests for a specific slug
    if('main-site' === $wp->request){

        #The URL to redirect TO
        $url = 'http://www.example/main-site/sub-site/';

        #Let WordPress handle the redirect - the second parameter is obviously the status
        wp_redirect($url, 301);

        #It's important to exit, otherwise wp_redirect won't work properly
        exit;
    }
}

Let me know if you have any questions.

It appears that the $wp->request that suggested in the above reply is always an empty string (in WPMS 4.5.2), so instead you can check this against is_main_site();.

The accepted answer is not working for Wordpress 4.9.8. Here is the updated and tested code. Put this inside function.php of the active theme.

<?php
function wpse66115_redirect_to_sub_site() {
  if ( is_main_site() ) {
    exit( wp_redirect( 'http://www.example/main-site/sub-site/', 301 ) );
  }
}
add_action( 'parse_request', 'wpse66115_redirect_to_sub_site' );
?>

The updated and tested code is neither working in 5.3. I found a solution by changing .htaccess:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^olddomain$ [OR]
  RewriteCond %{HTTP_HOST} ^www.olddomain$
  RewriteRule (.*)$ http://www.newdomain/$1 [R=301,L]
</IfModule>

本文标签: Redirect Main Site to Subsite in Multisite Wordpress