Redirect to a Different URL using .htaccess

Basic Redirect

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

redirect accessed-file URL-to-go-to

There are 3 parts;

(1) the Redirect command, 
(2) the location of the file/directory you want redirected, and 
(3) the full URL of the location you want that request sent to. 

These parts are separated by a single space and should be 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 going to 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 going to send the 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 tilde URL (i.e. /~account/) to an existing virtual host

These instructions apply to accounts with virtual hosts. If you do not have a virtual host and would like to get one please see the Virtual Host Name Service.

You can redirect a tilde URL, such as www.indiana.edu/~account/ to the virtual host name 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, you can simply remove "[OR]" from line 2 and all of 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 setup a redirect, you should test it by going to the tilde URL.

You can also redirect users using Meta tag http-equiv refresh. For instructions, see Using the Meta Tag "Refresh" to Redirect to a Different URL.

Redirecting from www.indiana.edu to www.iu.edu

If you are looking to redirect from www.indiana.edu/~account/path/to/page.php to www.iu.edu/~account/path/to/page.php, you can set up 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 3 occurrences of ~account with the name of the actual account.

Redirecting HTTP to HTTPS

Include a .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]