Sheerwood John Caday
Sheerwood John Caday

Reputation: 229

how to set a header.php redirect in wordpress?

i just want to ask how to set a redirect url in header.php? it seems I am having a redirect loop.

<?php
header("Location: http://www.sitename.com/category/videos/");
?>

Upvotes: 0

Views: 4239

Answers (3)

Jimmy Langat
Jimmy Langat

Reputation: 11

Place the following HTML redirect code in a WordPress Page or Post:

    <meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourURL.com/index.htm"> 

This code tells your browser to auto-refresh the current Web page. When it does, it loads the URL in the "url" string. The "0" attribute associated to "content" determines how many seconds before the redirect takes place. The attributes associated with "content" and "url" are the only ones you should alter.

Upvotes: 0

irfanengineer
irfanengineer

Reputation: 1300

You Can put you code directly in header.php or

if($_SERVER['REQUEST_URI']=="your URL When you want redirect")
{
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.sitename.com/category/videos/");
exit;
}

Upvotes: 0

xphan
xphan

Reputation: 1058

You should use wp_redirect instead of the php header() function. Hook the call to the wp_loaded to prevent the "headers already sent"-error.

Example: (add to functions.php)

add_action ('wp_loaded', 'my_redirect_function');

function my_redirect_function() {
    // define when the redirect should be made
    // example: only redirect for the page with slug "about-me"
    if(!is_page( 'about-me' )){
        return;
    }

    // define your url here
    $url = 'http://google.com';
    wp_redirect($url);
    exit;
}

Upvotes: 2

Related Questions