i'm a little worried here.

I'm trying to redirect EVERYONE that access my site to a specific webpage, and nothing is working! Currently i'm trying this code:

function redirect_this() {
    wp_redirect( 'http://www.myawesomesite.com/here.html', 301 );
add_action('template_redirect', 'redirect_this');

And the page always show

Warning: Cannot modify header information - headers already sent by ...

This is driving me crazy.

By the way, i've created a plugin to hold this code. So this code is in a single plugin file.

  • Could you show the full warning? The part after "headers already sent by ..." could be interesting. – Geert May 8 '11 at 17:25

Better use a .htaccess redirect so you avoid even loading WP:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com [R=301,L]

If you want to keep the path after the domain, (e.g. www.olddomain.com/mypage -> www.newdomain.com/mypage) replace the last line with:

RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
  • Save three bytes: RewriteRule ^ http://www.newdomain.com [R=301,L] – Geert May 8 '11 at 17:28

template_redirect is happening after the headers are sent, try hooking redirect_this into wp_loaded instead:

add_action('wp_loaded', 'redirect_this');

You'll have to be a little more deliberate in your redirect_this function to specifically target the page you want to be redirecting, or else everything will redirect to that page, do something like this:

function redirect_this() {
    if ( is_page('slug-of-page-you-wish-to-redirect') ) {
        wp_redirect( 'http://www.myawesomesite.com/here.html', 301 );
  • 2
    The template_redirect hook is not happening after headers are sent normally. I've been able to do successful wp_redirect()s via that hook. In this case wp_loaded works as well, though. – Geert May 8 '11 at 17:27
  • 1
    template_redirect is not called after headers are sent (in the "usual"). If headers are already sent by template_redirect there is a problem. – Joe Hoyle May 8 '11 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.