How To Redirect Users Using Javascript or PHP!

October 27th, 2009

code

Say your switching domains and you want to automatically forward users that go to your old domain, to get forwarded them to your new one. This can be accomplished with either Javascript or PHP.

Using Javascript

Insert this Javascript code into the head of your page (Most likely your index file):

<script type="text/javascript">
function delayer(){
    window.location = "http://joshuaschaible.com"
}
</script>

Replace “http://joshuaschaible.com/” with that URL you want to forward your users too. Then you must insert an “onload” function in your body tag. It should look like this:

<body onLoad="setTimeout(‘delayer()’, 5000)">

This tells the browser to forward to the site you specificed in 5 seconds. 5 Seconds = 5000. 4 Seconds = 4000 and so on.

Using PHP

If you want to automatically forward your users with out a delay, here is a simple step. First your index file must have an extention of .php. (ex. index.php) If it doesn’t you can do this in your control panel or FTP. Next, insert this code at the top of your web page.

<?php
  header ("Location: http://joshuaschaible.com");
?>

Again, make sure to change the URL so that your site doesn’t forward to mine.

Tags: ,

Leave a Reply