Use the .htaccess file on Sitehost to redirect to a different URL
On this page:
Basic redirect syntax
On Sitehost, you can use the .htaccess
file to redirect users to a different URL. In most cases, you should do this in the root web
directory. UITS recommends editing the .htaccess
file directly on the SSH server rather than through external site builder tools such as the WCMS. 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 or directory you want to redirect
- The full URL of the location to which you 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 web
directory of your account to newpage.html
, the syntax should be:
redirect /oldfile.html http://<account>.sitehost.iu.edu/newpage.html
Anyone who visits http://<account>.sitehost.iu.edu/oldfile.html
will end up on http://<account>.sitehost.iu.edu/newfile.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 /old_dir/ http://<account>.sitehost.iu.edu/new_dir/
Anyone who visits http://<account>.sitehost.iu.edu/old_dir/filename.html
will end up on https://<account>.sitehost.iu.edu/new_dir/filename.html
.
Redirect a virtual host root site to a folder
Most virtual hosts point to the web
folder in a Sitehost account. For the virtual host to point to a different folder within the Sitehost 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/$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.
This is document aytj in the Knowledge Base.
Last modified on 2023-07-17 14:52:48.