Dipendra Pokharel
Dipendra Pokharel

Reputation: 507

Redirect only WordPress homepage with a 301 redirect

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?

Upvotes: 3

Views: 23927

Answers (8)

Tony
Tony

Reputation: 1

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

Upvotes: -3

Ahmed Alaa
Ahmed Alaa

Reputation: 11

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

Upvotes: 1

Spencer Cameron-Morin
Spencer Cameron-Morin

Reputation: 1430

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

Upvotes: 22

Neel
Neel

Reputation: 19

You can use is_home() or is_front_page() function to redirect your home page.

Upvotes: 0

Faishal
Faishal

Reputation: 1493

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

Upvotes: 1

Tony M
Tony M

Reputation: 8302

You can use a Wordpress function to detect if you're on the home page:

is_home

http://codex.wordpress.org/Function_Reference/is_home

<?php
if ( is_home() ) {
    // This is a homepage - 301 Redirect
    header("HTTP/1.1 301 Moved Permanently"); 
    header("Location: http://www.example.com/np");
    exit;
} else {
    // This is not a homepage
}
?>

Upvotes: 0

diggy
diggy

Reputation: 6828

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

Upvotes: 4

Niels Keurentjes
Niels Keurentjes

Reputation: 41958

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.

Upvotes: 4

Related Questions