ARCHIVED: Use the .htaccess file on Webserve to redirect to a different URL

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.
Note:

Due to the evolving coronavirus situation, the retirement of Webserve was delayed until April 16 to allow classes that reference sites on Webserve to continue with minimal disruption.

All remaining Webserve sites were disabled beginning April 16, along with login access to webserve.iu.edu. If you have websites that still need to be moved to IU Sitehosting, you should migrate immediately.

On Webserve, you can use the .htaccess file to redirect users to a different URL. The most basic .htaccess file looks for any request for a specific page and, if it finds that request, forwards it to a new page you specified. The syntax is:

redirect accessed-file URL-to-go-to

The syntax has three parts:

  • The redirect command
  • The location of the file/directory you want to redirect
  • The full URL of the location you to which want to send the request

These parts are separated by a single space on one line.

For example, if you want to redirect users from oldfile.html in the www directory of your account to newpage.html, the syntax should be:

redirect /~account/oldfile.html http://www.indiana.edu/~account/newpage.html

Anyone who visits http://www.indiana.edu/~account/oldfile.html will end up on http://www.indiana.edu/~account/newpage.html.

You must use a full URL, even if you're sending users to another page on your own site.

You can also redirect an entire directory:

redirect /~account/old_dir/ http://www.indiana.edu/~account/new_dir/

Anyone going to http://www.indiana.edu/~account/old_dir/filename.html will end up on http://www.indiana.edu/~account/new_dir/filename.html.

Redirect a tilde URL (for example, /~account/) to an existing virtual host

These instructions apply to accounts with virtual hosts. If you don't have a virtual host and would like to request one, see the ARCHIVED: About virtual hostnames on Webserve.

You can redirect a tilde URL, such as www.indiana.edu/~account/, to the virtual hostname associated with the account by using an .htaccess with this code:

Note:
www alias also redirects to the virtual host.

For example, www.vhostname.indiana.edu would redirect to vhostname.indiana.edu. If you want to remove this redirect, simply remove "[OR]" from line 2 and delete line 3.

RewriteEngine on RewriteCond %{REQUEST_URI} ^/~account/ [OR] RewriteCond %{HTTP_HOST} ^www.vhostname.indiana.edu$ RewriteRule ^(.*)$ http://vhostname.indiana.edu/$1 [R,L]

After you've set up a redirect, you should test it by going to the tilde URL.

Note:

If you are using a virtual host that does not have SSL and want to redirect users to a different virtual host that has SSL, this must be done in two stages:

  1. Redirect traffic from your non-SSL virtual host to the HTTP version of the other virtual host <<http://virtualhostname.indiana.edu>>.
  2. In the www folder of the Webserve account hosting the other virtual host, create a HTTP to HTTPS redirect.

Attempting to redirect directly from the non-SSL virtual host to a virtual host with SSL will break the redirect rule and cause loops and other issues.

You can also redirect users using the meta refresh tag. For instructions, see ARCHIVED: Use the meta refresh tag on Webserve to redirect to a different URL.

Redirect a virtual host root site to a folder

Most virtual hosts point to a www or wwws folder in the webserve account. For the virtual host to point to a different folder within the webserve account, you need to use a rewrite condition, as redirects in .htaccess require specific files and will not work with URLs.

To redirect a virtual host (for example, virtualhostname.iu.edu) to the folder you want, use this .htaccess code:

RewriteEngine On

RewriteCond %{HTTP_HOST} virtualhostname.indiana.edu

RewriteCond %{REQUEST_URI} !^/folderinyourwwwfolder

RewriteRule ^(.*)$ https://virtualhostname.indiana.edu/folderinyourwwwfolder/index.html$1 [L]

After you've set up the redirect, you should test it by going to the virtual host URL. If it fails, you may need to clear your browser cookies. If you are still having trouble, one of the following may be the case:

  • You are not using full URLs.
  • Your capitalization in your folders may be different than the capitalization you are using in your rewrite rules.
  • You are using https to direct to something in your www folder.

Redirect from www.indiana.edu to www.iu.edu

If you want to redirect from www.indiana.edu/~account/path/to/page.php to www.iu.edu/~account/path/to/page.php, set the following in your .htaccess file:

RewriteEngine on # remove trailing slash RewriteRule (.+)/$ /~account/$1 [L,R=301] # add trailing slash RewriteCond %{REQUEST_FILENAME} !-f RewriteRule .*[^/]$ /~account/$1/ [L,R=301] # perform redirect if indiana.edu RewriteCond %{HTTP_HOST} ^www.indiana.edu$ [NC] RewriteRule ^(.*)$ http://www.iu.edu/~account/$1 [R=301,L]

Replace the three occurrences of ~account with the name of the actual account.

Redirect HTTP to HTTPS

Include an .htaccess file with the following rules in your www folder in order to direct incoming traffic from HTTP to HTTPS:

  RewriteEngine On

  RewriteCond %{HTTPS} !on$

  RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

This is document bfrf in the Knowledge Base.
Last modified on 2021-09-08 10:23:38.