Using the wp_redirect() function
Did you know that WordPress provides a function which allows you a safe way to redirect to any URL?
It's a pluggable function called
wp_redirect() which takes two parameters ($location and $status).
So what's a good use case? Say you've got an old page template (old-page) and you want to redirect visitors to the new page. Not only that, you want it to be a permanent redirect which means Google transfers your page rank & inbound link juice to the new page.
Here's how you would do it.
Edit your page template and at the top, paste in the following code:
PHP Code:
<?php
wp_redirect( 'http://www.mysite.com/new-page/', 301 );
exit;
?>
The first parameter is the new url you want to redirect visitors to. The second parameter is the type of redirect. 301 means "permanently moved" which is important to tell search engines. (see wikipedia for
all http status codes)
Just make sure that the function is called before any output is sent to the browser otherwise you'll likely get a "headers already sent" error.
Without this function, the old method would be to use the following PHP code instead:
PHP Code:
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.mysite.com/new-page/");
?>
That's it. Now you can easily use the safer WordPress redirect function instead of the old php header method.
For full function reference, see the
wp_redirect() page in the WordPress Codex.