How to redirect web pages using a very simple PHP code

PHP Logo

This is  a very short and simple tutorial on how to redirect your web pages using a simple php script. You may need to do simple page redirection for quite a few reasons. For example;

1) You’ve change your domain name and the old url no longer exists so when the user types in the old url they seamlessly get redirected to the new one

2) Users are redirected to browser specific web pages

3) Location specific web pages, e.g. a user who lives in the Dominican Republic who tries to access google.com will automatically be redirected to maybe google.com.do where the site is in spanish.

Do not use redirects if the destination page is completely different from what the user might be expecting. So, if you have a link that says James Bond movies the destination site should not end up being entirely different content. This won’t be good for search engine rankings and will not be good for user experience.

Page Redirect using PHP

Web page redirection using php is very simple. Simply add the following code at the beginning of your web page file, before the <html> tag. This needs to be first on the page. The safest way to do this is to remove all other content from page, that way no mistake can be made.

<?php
header( ‘location: http://www.yoursite.com/newpage.html’ ) ;
?>

You will need to replace the URL above with the URL you wish to direct to. Also, remember to save the file that contains this PHP code with a .php extension. This tells the server to expect php code within the file and therefore parse it accordingly. That’s it.

Note: When using the ‘Back’ button, the user is taken to the last viewed page and not the redirect page.

In summary, there is nothing wrong with the auto-redirecting method, provided that the final destination contains what the surfer expected to see.

Robyn-Dale Samuda is a Web Developer & Entrepreneur & is CEO of Creative Engine Jamaica - Web Design. He has a passion for the web and loves offering assistance and inspiration whenever possible and does so through Sam's Web Guide. He is also a Writer for the popular blog, Blogging Pro. Follow him on twitter Here

Got something to say? Please share it with us below!