Redirect only WordPress homepage with a 301 redirect

18,615

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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

Solution 5

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
Share:
18,615
Dipendra Pokharel
Author by

Dipendra Pokharel

Jack of all, master of none. Working as a full stack JAVA developer. https://dipen.codes

Updated on June 05, 2022

Comments

  • Dipendra Pokharel
    Dipendra Pokharel almost 2 years

    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.

    What I want is to redirect example.com homepage only to example.com/np. It would be great if that redirection is a 301 moved permanently redirection.

    If I place the 301 redirection in WordPress header file, header.php, it will redirect every page. And if I check if the page is home and try a 301 redirection that's not possible because header redirection should be placed at top.

    How to achieve this?

  • Dipendra Pokharel
    Dipendra Pokharel almost 11 years
    This is possible for normal redirection but as far as I know 301 redirection is not possible within this code. Please correct me if I am incorrect.
  • Tony M
    Tony M almost 11 years
    This is probably the best answer - if you plan on keeping your theme up to date, you won't have to worry about overwriting code. +1
  • Spencer Cameron-Morin
    Spencer Cameron-Morin almost 11 years
    It's actually more contextually appropriate to hook on template_redirect: codex.wordpress.org/Plugin_API/Action_Reference/…
  • Spencer Cameron-Morin
    Spencer Cameron-Morin almost 11 years
    You haven't answered his question, though. He wants to know how to redirect from his homepage, not just determine if the homepage is requested. :)
  • Spencer Cameron-Morin
    Spencer Cameron-Morin almost 11 years
    WordPress provides a function for this wp_redirect: codex.wordpress.org/Function_Reference/wp_redirect
  • Spencer Cameron-Morin
    Spencer Cameron-Morin almost 11 years
    I'd rather see wp_redirect used and keep the redirect code in a child theme. Updating your theme code won't be an issue if you use a child theme, which is the recommended way of altering theme behavior: codex.wordpress.org/Child_Themes .htaccess could also be overwritten simply by an update to the permalink structure.
  • Niels Keurentjes
    Niels Keurentjes almost 11 years
    Means there was a RewriteBase / in there - I couldn't know ;)
  • NineCattoRules
    NineCattoRules almost 9 years
    I set this rule and first it doesn't work and second it breaks my website. I have an MU installation
  • Raptor
    Raptor over 4 years
    Never change WordPress core codes. It might break after version upgrade.