Redirect only WordPress homepage with a 301 redirect

后端 未结 8 597
天命终不由人
天命终不由人 2021-01-14 21:53

I have a blog, lets say example.com and another blog installed in example.com/np which is not multisite but a different WordPress installation.

相关标签:
8条回答
  • 2021-01-14 21:57

    Put the following in your functions.php file:

    add_action( 'get_header', 'so16738311_redirect', 0 );
    function so16738311_redirect()
    {
        if( is_home() || is_front_page() ) {
            wp_redirect( home_url( '/np/' ), 301 );
            exit;
        }
    }
    
    0 讨论(0)
  • 2021-01-14 22:04

    You can use wp_redirect function of WordPress with status code. add Follwing code on wordpress init hook

    if ( is_home() ) {
        wp_redirect( $location, $status );
        exit;
    }
    
    0 讨论(0)
  • 2021-01-14 22:07

    Just put this at the very top of your index.php file of your wordpress installation:

    if($_SERVER['REQUEST_URI'] == "/"){
        include('......server file directory to file you want to redirect to');
        exit;
    }
    
    0 讨论(0)
  • 2021-01-14 22:11

    Since you're in the context of WordPress, you can utilize its redirect functionality.

    Like this ( in functions.php ):

    function redirect_homepage() {
        if( ! is_home() && ! is_front_page() )
            return;
    
        wp_redirect( 'http://redirect-here.com', 301 );
        exit;
    }
    
    add_action( 'template_redirect', 'redirect_homepage' );
    
    0 讨论(0)
  • You don't want to muddle in your Wordpress templates or code for this, just use a single mod_rewrite rule in your .htaccess:

    RewriteRule / /np [R=301,L]
    

    Put it below the RewriteEngine on line if it's already there, else add it as a separate line above the RewriteRule line.

    This solution is easily removable, easily maintainable, portable, and performs better than doing it in PHP in the templates or WP code, and survives updates to your templates.

    0 讨论(0)
  • 2021-01-14 22:17

    Options +FollowSymLinks
    
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule (.*) http://www.Your domain/$1 [R=301,L]
    </IfModule>
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

    0 讨论(0)
提交回复
热议问题