A PHP 301 Redirect
Posted on Monday, May 14th, 2007
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.
If you enjoyed this post, why not subscribe to Unintentionally Blank
Pingback by Snippet of highest usefullness at Christian Holmes
May 16th, 2007 at 6:29 pm
1December 13th, 2007 at 8:54 am
Dainu tekstai Says:Thanks for it. Very useful. But there is any solution with PhP to leave previos url when page redirect to new page?
2December 13th, 2007 at 7:23 pm
Phil Says:Dainu,
Thanks for commenting. Cloaking links like that is bad practice as it is important for users to know where they are. Otherwise they could end up on any number of sites that wish them and their computers/email/bank accounts harm.
A 301 redirect like this just allows you to move pages without losing link weight and stays transparent for users as well, I wouldn’t advise using any other redirect (but for a 302 redirect when it is temporary).
3December 19th, 2008 at 12:22 pm
dypa Says:u need to use die() too
sorry for my english
4September 2nd, 2010 at 6:47 am
Yurtdışı Eğitim Says:Thanks for your useful tips. I will use on entry of my web page.