Title: Where does WP write headers?
Last modified: August 18, 2016

---

# Where does WP write headers?

 *  [justpetehere](https://wordpress.org/support/users/justpetehere/)
 * (@justpetehere)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/where-does-wp-write-headers/)
 * Hey all,
 * I’m trying to figure out the best place to put the following piece of code, or
   how to best modify it:
 * `if (is_404()) {
    if (file_exists('/home/justpete/archive' . $_SERVER[REQUEST_URI])){
   header("HTTP/1.1 301 Moved Permanently"); header("Location: http://archive.justpetehere.
   com" . $_SERVER[REQUEST_URI]); exit(); } }
 * I just replaced my previous blog with WP, and decided to not import it, rather,
   just move it over to an “archive” hostname on the same server. This script checks
   if the file requested exists on the archive hostname, and if so, sends a 301 
   to redirect. I also want to have it check if somehow the page ALSO exists on 
   the WP blog, so the if is_404 would be nice.
 * The problem is that I can’t figure out a place in the calls before PHP defines
   the header for the document.
 * Anyway, any ideas?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [19 years, 11 months ago](https://wordpress.org/support/topic/where-does-wp-write-headers/#post-419930)
 * Headers are not sent until some other actual text is sent. So even though the
   header has been “set” as it were, you can still override it as long as no actual
   output has been done yet.
 * Best way to do that that I can think of is to make a 404.php in your theme directory.
   This gets called when wordpress has a 404 error page to display.
 *  Thread Starter [justpetehere](https://wordpress.org/support/users/justpetehere/)
 * (@justpetehere)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/where-does-wp-write-headers/#post-419950)
 * Otto,
 * Unfrotunately that’s not working. I tried just placing the header lines without
   the if statements in the index.php of my template with the end();. I just end
   up with a blank page. And I’m assuming that it does define the content-type header
   at some point, as there are functions specifically for the purpose.
 * Any other ideas?
 *  [geoffe](https://wordpress.org/support/users/geoffe/)
 * (@geoffe)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/where-does-wp-write-headers/#post-420449)
 * Not all requests for files would be going through the index.php file in your 
   template folder. They won’t necessarily be going through a header.php template
   either.
 * Unfortunately, making your redirect in the 404.php template file of your theme
   won’t be much help because it has already sent the 404 header and you want it
   to get a 301.
 * You’ll need to get into the [WP_Rewrite](http://codex.wordpress.org/Function_Reference/WP_Rewrite)
   workings and take a look at the parse_query function and perhaps the rewrite_rules
   function in wp-includes/classes.php to do some root_rewrite_rules filter to get
   the effect you want.
 * However, I’d warn that what you want to do is potentially terribly inefficient
   for your server.
 * Edit: look into doing your function through the handle_404() in the WP object
   at the bottom of classes.php

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Where does WP write headers?’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [redirect](https://wordpress.org/support/topic-tag/redirect/)

 * 3 replies
 * 3 participants
 * Last reply from: [geoffe](https://wordpress.org/support/users/geoffe/)
 * Last activity: [19 years, 11 months ago](https://wordpress.org/support/topic/where-does-wp-write-headers/#post-420449)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
