You are here: Foswiki>TIG Web>WebServices>SendingRedirects (26 Aug 2010, edemaine)EditAttach

Why send HTTP redirects?

It is frequently desirable to direct Web clients requesting one URL to another. For example, if you maintain a Web site for a course that is offered regularly, you probably want to have new content for each term that the course is offered. But you listened to Tim B-L and know that cool URIs don't change, so you created subdirectories for each term. Now when students go to the top level of the course Web site, they get a directory listing of all the previous terms. You want them to go directly to the current term's pages.

The answer is to issue a redirect. More specifically, you want to issue a temporary redirect, which tells the browser "the content for the thing you requested is now over here", as opposed to a permanent redirect ("the thing you want has moved and here is the new name"). Doing this correctly is quite simple on the CSAIL Web servers. Unfortunately, doing it incorrectly is even easier. It will be better for the Internet as a whole if as many people as possible do it the right way.

Most people's first try at this is to create an index.html file in their top-level directory. That hides the extraneous content, but doesn't actually redirect the user (because there is no way to issue a redirect from plain HTML). It is possible to issue a redirect from PHP or from a CGI script, if you're already using dynamic content, but that's a big hammer to use on this problem. So many users (helped by a lot of resources on the Web which say to do it this way) hit on the idea of having a "refresh" in their index.html file, which instructs the browser to go to a different URL. This is usually set up to refresh immediately, which looks like a redirect to the user (until they try to use their browser's "back" button) but actually means something completely different.

How to send HTTP redirects from a CSAIL Web site

We have found that the simplest method is as follows. In the most common case, you want to redirect from some directory (let's say it's / to a subdirectory of that same directory, but without breaking URLs that refer to other subdirectories. Create an .htaccess file in the top-level directory (usually .../www/data/), or add to an existing .htaccess file, the following:
RewriteEngine on
RewriteBase /
RewriteRule ^$ fall09/ [R=temp]

In place of the directory fall09/ in the RewriteRule command above, you can also supply a complete URL (which you might do, for example, if you have decided to move your course to Stellar). You can also change the temp to permanent if you will no longer be using or publishing the CSAIL URL. In the unusual case where your content lives at the "top level" of the Web server, you must omit the RewriteBase directive.

-- GarrettWollman - 12 Jul 2007

Redirecting specific paths

What if you want multiple URIs to go to the same place? For example, at first you create a webpage /, but later you realize you really wanted a directory /, but you don't want to break old links. In this case, you can use the simpler Redirect mechanism in your .htaccess file:
RedirectPermanent / /
You can omit the Permanent if you think this might change in the future.

Another example: you want / to redirect to /6.yyy/fall09, because back in 2009 the now-permanent class number was known as temporary class number 6.yyy. Then you can use
RedirectPermanent / /6.yyy/fall09

-- Main.edemaine - 26 Aug 2010
Topic revision: 26 Aug 2010, edemaine

MIT Computer Science and Artificial Intelligence Laboratory


  • About CSAIL
  • Research
  • News + Events
  • Resources
  • People

This site is powered by Foswiki MIT: Massachusetts Institute of Technology