Redirecting URLs from Your Web Space

Sometimes, web maintainers need to 'move' a URL. Sometimes it's because an actual file has been moved (as when a class project is moved to a departmental page), and sometimes it is for convenience, as when the web designer wants users to be able to access their main page by using the URL of the directory. At Lehigh, this often happens because a user wants to use the 'short' version of their URL (giving only a directory name, not a file name): http://www.lehigh.edu/~userid/.

These kinds of redirections can be done by including a directoryindex or redirection statement in the .htaccess file for the directory. The .htaccess file allows the designer to control who can access the files in the directory and what kind of files are being served (see Serving Extraordinary Documents, Technical bulletin 15).

The .htaccess file is simply a plain text file. It can be created using edit (dte), vi, or emacs. The file name must be .htaccess

In most cases, you must create .htaccess file in the web directory you want the redirect to come from. So, if you want to redirect the URL: http://www.lehigh.edu/~userid/junk to http://www.lehigh.edu/~userid/junk/myjunk.html, you would put the .htaccess file in your web space, in the subdirectory /junk.

Note: .htaccess files are not visible when doing the scan command on the Network Server. You must use the shell command, cd to the directory, and enter ls -la to check for a .htaccess file.


Index.html Files

The simplest example of a redirection is the index.html file. When a user accesses a URL that points to a directory, the index.html file in that directory is displayed, or, if there is no index.html file, the file listing of that directory is displayed.

To keep the whole world (wide web) from viewing your directories, you must create a HTML file called index.html in each one.

Note, however, that all AFS users (those logged on to a Network Server, Compute Server or AIX workstation) will be able to view the files in your www-data directory through those machines, because the files are in your AFS public space.


Default File Names

Suppose you want the default for a directory to go to a different file than index.html-- for instance, userid.html. The DirectoryIndex command, placed in your .htaccess file, will allow you to choose a different directory index.

To set a directoryindex for a local directory, put an .htaccess file in the directory, with a DirectoryIndex directive in it:
DirectoryIndex filename

So that if you want to have the file mypage.html come up when your main directory is accessed, you would put
DirectoryIndex mypage.html
in the .htaccess file in your main directory.

DirectoryIndex options are inherited-- the webserver will look for that file as the default in all subdirectories.

However, you can list multiple filenames in the DirectoryIndex command. So if you want the default in your main directory to be mypage.html, and in all other directories to be index.html, the directive would be:
DirectoryIndex mypage.html index.html

The server will look for the file names in the order in which they are listed, so it will look for mypage.html first, and if there is no mypage.html, look for index.html.

You can also set a DirectoryIndex directive in the .htaccess file in any subdirectory, which will override inherited directives.


Redirecting URLs/File names

If you have moved files, or you want calls to one file name to go to another, you can make this happen using a Redirect directive.

To redirect a call to one file to another instead, add a Redirect directive in your .htaccess file:
Redirect /~userid/filepath URL
Where filepath is the path and file name you want to redirect from (inside your web space), and URL is the full URL you want to redirect to. [You must always include the /~userid/ in your path.]

Or, instead, to redirect the userid.html file to your index.html file:
Redirect /~userid/userid.html http://www.lehigh.edu/~userid/index.html
Then, when someone clicks on your entry in the home pages listing, http://www.lehigh.edu/~userid/userid.html, they will go to your index page, http://www.lehigh.edu/~userid/index.html

You can also use a redirect to redirect calls to index.html to the userid.html file, though using a DirectoryIndex directive is preferred:
Redirect /~userid/index.html http://www.lehigh.edu/~userid/userid.html
Then, when someone accesses your home directory:
http://www.lehigh.edu/~userid/, they will go to your userid page,
http://www.lehigh.edu/~userid/userid.html

You can redirect a directory to another directory, and all calls to files in that directory will go to the other:
 


This page created and maintained by
Jennifer Heise jahb@lehigh.edu

 

(Last modified on February 19, 2001)