Site Redirect

Setting up a new web site using "free" hosting is a good way to start testing and building up your first site without it costing you any money. You will not want to keep it on that hosting though as the reason they can offer the hosting "free" is because they are collecting all the income that can be earnt from the site instead of you getting the income from the site. Eventually, as your site grows and starts to become successful it will reach the point where you can buy hosting for a fraction of the income that your site can earn.

At that stage in the development of your site you will want to move it onto its own paid hosting. This will most likely mean that the domain name where your site can be found will change because few "free" hosts allow you to have your own domain name and you will want your own domain so as to clearly identify your site.. Almost certainly, the free hosting that you are moving from will not allow you to set up the server side redirects that you would use to redirect one web page to another when you restructure your site on subsequent occasions. This means that you will not be able to hide that the original address points to"free" hosting when you redirect to your new domain on your paid hosting. There are however a couple of ways that you can redirect visitors from your original site to the new one so that they will at least know that you have moved.

One way to handle this redirection is using the HTML meta tag.

<meta name="refresh" content="0;url="/>

Adding this tag at the very top of the head of your page will redirect to your new site as soon as the page loads enough to read this instruction. It is probably going to be against the terms of service of the "free" hosting to use an immediate redirect though because doing so uses up their bandwidth without giving them the opportunity to earn anything from the display of your page. We can resolve this by adding a delay of a few seconds where the original page displays before redirecting to the new copy of the same page. If we change the 0 into say 5 then we add a five second delay before the redirect. There is a problem with coding this though because it means that you have to change the HTML that you have in every single page on your site individually to reference the corresponding page on the new location. If there are dozens or hundreds of pages on your site then this will be a big task.

If you are using JavaScript then you may already have a common function library that is linked into all of your web pages. If this is the case then adding a JavaScript redirect into the external file already linked into all your pages means that you can add a redirect to all your pages (no matter how many of them there are) in the one small change.

All we need to do is to have the script identify which page we are on and redirect to that page at the new location. The location object in JavaScript not only makes it easy to grab the part of the address that identifies the current page but it can also capture any hash value or querystring added to the address and pass those along as well.

In order for the script to redirect immediately to the new page we would need the JavaScript to be attached to the head of the page. This is one of the very few instances where that is the appropriate place to attach JavaScript and so the script is more likely to be attached to the bottom of the body. Since an immediate redirect is probably going to breach the terms of service we'll insert a delay before redirecting and now the placement of the script within the page (either in the head or bottom of the body) is no longer relevant.

The following code added into any external JavaScript that is attached to all our pages will redirect them to the corresponding page at the new location after 5 seconds (the number in the second parameter of a setTimeout call is the delai in 1/000ths of a second).

setTimeout(function() {location.href = '' + location.pathname + location.hash +;}, 5000);

Of course you will want to specify your new domain name where I put in the above code. You will also need to make sure that all of your pages are copied to the new location exactly as on the old site except that the new copy of the pages should not include this redirect code or you will set up an infinite redirection loop.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow