Penn Engineering CETS Answers

How do I add a redirect to a web page?

Why would you want to do a redirect? Maybe you have changed the URL of your homepage. You can put a redirect on the old URL, to send any visitors to your new homepage URL.

There are 3 ways to do a redirect.

1. meta tag refresh

2. regular <a href> link

3. javascript

You can use all three ways or only one way. It's best to use all three.


Using a Meta Tag Refresh and Regular <a href> Link

The following code gives an example of a basic page using a meta tag refresh and a regular <a href> link.
------------------------------------------------------
<HTML>
<HEAD>
<TITLE>Meta Redirect Code</TITLE>
<meta http-equiv="refresh" content="3;url=http://www.myhomesite.com/pictures">
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<P><TABLE WIDTH=600 align=center>
<TR>
<TD>
<P>You are being re-directed to my pictures site at <A HREF="http://www.myhomesite.com/pictures">
http://http://www.myhomesite.com/pictures</A><br>
<BR>
If you are not automatically redirected, please click <A HREF="http://www.myhomesite.com/pictures">HERE</A>
</P>
</TD>
</TR>
</TABLE>
</P>
</BODY>
</HTML>

-----------------------------------------------------

<meta http-equiv="refresh" content="3;url=http://www.myhomesite.com/pictures">
In the above code, the content="3 is the number of seconds the page takes to redirect. You can change this to 0 seconds if you wish.


Using Javascript

The third way to do a redirect is using javascript.
-------------------------------
<script language="javascript">
location.replace("http://www.yournewsite.com");
</script>

------------------------------

First, a JavaScript script will try to REPLACE the redirecting page with the new address. The reason you want it to try to REPLACE the page instead of redirecting it, is because it wont break the back button. Meaning, if it redirects to a new address instead of replacing it, if you were to hit the back button in your browser once you've been redirected, you will go back to the redirecting page, and the redirecting page will redirect you back to the new address.

So instead, it will REPLACE the page, so when you hit the back button on your browser, it will send you to the last page you visited.

If the user has the javascript turned off in their browser then the meta refresh redirect will launch. That's why it's good to use all three methods of redirect.

 

© Computing and Educational Technology Services cets@seas.upenn.edu 215.898.4707