Moving URLs: 301 Redirects and GoogleCategory: PHP
Reviewed by: redemption
Reviewed on: Aug 02 2006
Thanks to the wealth of links and referrals on the web, URLs are unlike any other address in the world. If you have a long time URL of a page that just has to be changed, renamed, or moved, you really have to do it properly or you can risk losing all sorts of traffic.
The recommended method is to use a page redirect. If you're using PHP you can do a simple redirect using:
However, just using the header tag will actually cause PHP to automatically issue a 302 "Moved Temporarily" status. If search engines encounter when spidering your pages they will not refresh their URLs.
Instead, use a 301: Moved Permanently redirect like this:
header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.foo.com/newurl.html");
Additional IE 5/6 supportApparently IE has a chance of misbehaving when using the header() redirect method outlined above. People have suggested (and I've used successfully) add an additional header ("Connection: close"); string to the whole thing. The final code would look something like:
header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.foo.com/newurl.html"); //add close header for IE header ("Connection: close");
Example implementationBasically the idea is to replace your original script code with the above header. Take for example your original script, http://www.foo.com/contact_form.php. Originally it might contain all sorts of form processing code. If you moved it to http://www.foo.com/contact.php you will simply replace all the code in contact_form.php with the redirect command.
An Alternative Method: Apache RedirectsApache can also redirect pages for you. Some people consider this the superior approach because you can embed it right into Apache' httpd.conf or .htaccess file and do away with the old file altogether. To do a redirect using .htaccess or httpd.conf you would use code such as:
redirect 301 /oldpage.html /newpage.html redirect 301 /oldpage.html http://www.foo2.com/newurl.html
All done!The 301 header will instruct Google, Yahoo, MSN and other search engines to relink to the newly directed URL on their next index refresh. This has several important bonuses, including retaining your Google pagerank!
This whole simple process will let you do two things:
- Make sure that links from outside your site are still functional with a redirect
- Instruct search engines to update their URLs.