How To Setup A Permanent Redirect (HTTP 301) With PHP

The HTTP 301 status code is meant for use when you want to redirect a page or URL to its new destination after reorganizing or moving a web site around. This can be on the same domain or an entirely new domain. Here is the W3C definition of the 301 code.

The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs. Clients with link editing capabilities ought to automatically re-link references to the Request-URI to one or more of the new references returned by the server, where possible. This response is cacheable unless indicated otherwise.

The new permanent URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).

If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
W3C Hypertext Transfer Protocol

This is particularly useful for search engines like Google, which will carry over page rank to the new page if this status code is seen. If you do not need to indicate permanent displacement, you can accomplish redirection by setting a Location header in PHP. The location header does the actual redirection to the new location, and can be used by itself.

If you set the Location header by itself, PHP automatically sets the status code to HTTP/1.1 302 Found.

Note, if you attempt to send headers after content has been sent, you will get a warning like, “Warning: Cannot modify header information - headers already sent by …”. Watch out for empty lines and spaces between PHP open and close tags.

Here is the code to do this in PHP…

// Permanent redirection  
header("HTTP/1.1 301 Moved Permanently");  
header("Location: [](");  

Just make sure you put these lines in your page before any other content is sent out. The part can be changed to whatever the new address for this resource is.