Unintentionally Blank

Phil Nash on the Internet, Web Standards and Accessibility

A PHP 301 Redirect

May 14, 2007

by Phil Nash

A friend pointed out recently that the subdomain my tests and examples are kept on had no index page in the root, so if anyone removed the page name from the address they could access my entire directory. I didn't think that this was a good idea, so I decided to change it. I could have added an index.html file or something else there to protect the directory, but I wanted to link it in with the rest of the blog and learn something at the same time.

I created a page on which I could display all of my tests and examples, it's the Test Centre that you can find linked above now. As a page in my blog, it is part of the main domain, so how do I connect the subdomain http://test.unintentionallyblank.co.uk to the page http://www.unintentionallyblank.co.uk/test-centre/?

301 Redirect

There are many ways of redirecting a page, and many that I am not going to talk about as there is plenty of information out there on which method is the best. As far as I have seen, the only redirect to perform is a 301 redirect. It means "permanently moved" and it works for browsers and search bots.

I decided to implement the redirect in PHP as it made sense to me. The code is simple, all you need to do is deliver a couple of new http headers to whatever was visiting the page you wanted to redirect, one to tell the visitor the page has moved and one to say where. Here it is:

<?php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: REDIRECT URL HERE" );
?>

That's all there is to it! In my case I entered the above into Notepad, saved it as index.php and uploaded it to the root of http://test.unintentionallyblank.co.uk, now visiting it brings you to the test centre at http://www.unintentionallyblank.co.uk/test-centre/. Try it out. If you want to replace other pages, just name your file accordingly and place it in the relevant location.

There are other ways to do this, different languages and scripts or editing the .htaccess file, but this seemed to be the easiest way to go for me, hopefully it can help you too.

Unintentionally Blank is Phil Nash's thoughts on web development from 2006-2008. Any code or opinions may be out of date.