Simple PHP Redirect in WordPress

Note: This post was originally written for my blog over at However, with the changes I’m making to that site, I figured the post would be of more use here on this site.

Update: there is also a WordPress function for redirects: wp_redirect()

Sometimes when using WordPress you want to include a non-Wordpress page in your main navigation while still maintaining the simplicity of the wp_list_pages() function. I know there are plugins that do this type of thing, but many of them are just overkill. I just needed one non-Wordpress page on my menu, so here’s what I did. First, I created a page from within WordPress and gave it a heading but left the body blank. My page was called “Purchase” but you can name it whatever you’d like. This is the name that will show up in your navigation menu. Then I went to the header.php file of my theme and added this at the top:

header("HTTP/1.1 301 Moved Permanently");
header("Status: 301 Moved Permanently");
header("Connection: close");
exit(0); // Optional, prevents any accidental output

Obviously, you’ll want to change the if statement to check for the blank page that you set up. Then change “” to whatever page you want the redirect to point to. Make sure that you put the code at the very top of the header.php file. If something else comes before it, it won’t work.

I first found this PHP redirect on  Steven Hargrove’s blog.  He gives examples of this in a number of different languages. However, I adapted it to WordPress and added the ” header( “Connection: close”);” line to get it working properly. I’d love to hear how you solve this little WordPress problem. If you have a different solution, please leave a comment below with a brief explaination. Thanks.

Update #1: If you want to make a temporary redirect (302), rather than a permanent one (301) use the code below:

header("Connection: close");
exit(0); // Optional, prevents any accidental output

Update #2: Algen asked if it was possible to create a delayed redirect. It is possible, using either PHP or HTML. As Dhruv pointed out in the comments, PHP handles it like this:

header( 'refresh:5;url=wherever.php' );

If you’d prefer to use HTML, it can be done like this:

<meta http-equiv="refresh" content="10;url=http://www.example.php">

Posted June 17, 2010 by Alex Mansfield

10 responses to “Simple PHP Redirect in WordPress”

  1. Karl says:

    Hi, Nice tip, works a treat, but how would you go about doing it on multiple pages, would you just keep repeating the steps, or can you creat if else steps, saving having all the code again in the header.


  2. John Fox says:

    Alex, worked like a champ. Thanks!

    that exit(0) was crucial.

  3. Yasinda says:

    It works.Thanks for your article…

  4. Dhruv says:

    Hey Alex, It’s possible to do delay redirect with PHP. Something like this will work.

    header( “refresh:5;url=wherever.php” );

    Correct me if I am wrong.

    • alexmansfield says:

      Thanks for the comment Dhruv! You are absolutely correct. I’ve updated the post to include your contribution.

  5. James says:

    I am looking for a way to make it so if someone/something goes to /sitemap.xml that it will send them to /sitemap/ and would like to make it a part of the theme

    So you know of a way to do that?

    • alexmansfield says:

      If you’re trying to create the redirect from within WordPress, you might want to give the “Redirection” plugin a try. I haven’t tested it with a sitemap.xml file though, so I can’t make any promises.

      You should also be able to create the redirect using your .htaccess file.

  6. joni says:

    Thank you very much :)!!

Leave a Reply

Your email address will not be published. Required fields are marked *