On Webserve, how do I redirect to a different URL using the .htaccess file?

Important:
On Webserve, PHP 7.1 will become the default version in spring 2018; however, PHP 5.6 will still be available until it is retired at the end of December 2018. For help specifying which version of PHP to use, see PHP server-side scripting language.

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.

Redirecting a tilde URL (i.e., /~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 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.

You can also redirect users using the meta refresh tag. For instructions, see On Webserve, how do I redirect to a different URL using the meta refresh tag?

Redirecting 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 (e.g., virtualhost.iu.edu) to the folder you want, use this .htaccess code:

RewriteEngine On

RewriteCond %{HTTP_HOST} virtual.indiana.edu

RewriteCond %{REQUEST_URI} !^/folderinyourwwwfolder

RewriteRule ^(.*)$ https://yourvirtualhost.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.

Redirecting 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.

Redirecting 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 2017-08-15 13:38:34.

  • Fill out this form to submit your issue to the UITS Support Center.
  • Please note that you must be affiliated with Indiana University to receive support.
  • All fields are required.

Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.

  • Fill out this form to submit your comment to the IU Knowledge Base.
  • If you are affiliated with Indiana University and need help with a computing problem, please use the I need help with a computing problem section above, or contact your campus Support Center.

Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.