How do I use a .htaccess file to set Web page options?

Overview

An .htaccess file is a text file that gives direction on how to handle specific page requests.  It can tell the server to display a custom "404 file not found" page, specify what type of password authentication is used to access a site, redirect an old URL to a new URL, block/allow certain IP address ranges, etc.  This file can be created by using any plain text editor, though it is easiest to complete in UNIX.  This article covers the following topics: requiring a password to gain access to a site and using a file other than index.htm or index.html as the site's start page.  Additional instructions on setting up a custom .htaccess file can be found by entering "htaccess" on your favorite search engine.

Instructions

Please note that student organizations can do these things to their websites, too.  Substitute the student organization's username and website for your personal website in these instructions to make changes to it.

Creating the .htaccess file

Begin by establishing a VPN connection to Purdue.  This is done by using the "Cisco Anyconnect Secure Mobility Client".  Most hosts at Purdue require a VPN connection to be active to allow access to the server.  After you are connected, use a UNIX, Linux, or Mac OS X terminal, (or a Windows SSH program), to connect to a campus UNIX server that has access to the career account (H: drive).  For example, 'guru.itap.purdue.edu'. 

From a Windows operating system:

  1. Open an SSH program (SecureCRT, PuTTY, SSH Secure Shell, etc.) and connect to the server 'mace.ics.purdue.edu' on port '22' using 'SSH2' (if asked) as well as the proper career account user name and password.  
  2. A download of SecureCRT is available from https://communityhub.purdue.edu/storefront/browse/securitysoftware.  
  3. For a guide to using SecureCRT, follow steps 1 through 4 here: How do I change the permissions on my web page using SecureCRT?.
  4. If this is the first time that a connection to the expert server has been made from this computer, it will require that its RSA key be allowed before entering the password (click 'Accept' or 'Accept and Save' when asked).  If this is the first time that this account has ever connected to the 'expert' server, it will be necessary to go through the expert account set-up and then wait about 10 minutes before continuing.

From a UNIX terminal (UNIX/Linux/Mac OS X)

  1. Open a new terminal (in OS X, Terminal is located in the 'Applications' folder under 'Utilities'), then type the following command: 'ssh username@guru.itap.purdue.edu' where 'username' is the proper career account username.  For example, if the username was 'jpublic' the proper command would be 'ssh jpublic@guru.itap.purdue.edu'.
  2. Press the 'Enter' key.
  3. If this is the first time that the 'expert' server has been accessed from this computer, it will be necessary to allow its RSA key.  Simply type 'Yes' in to the command prompt if asked and enter the proper career account password when prompted.
  4. If this is the first time a connection to the 'expert' server has ever been made with this account, it will be necessary to go through the 'expert' account set up and wait approximately 10 minutes before continuing.

Regardless of what kind of system is being used, the instructions will be the same from this point forward. 

  1. Begin by changing to the folder where it is preferential to set webpage options.  If it is preferred to change options for the entire site, change to the 'www' folder.  If it is preferential to only change the settings for a part of the website, change to the folder within 'www' where that part is located.  
  2. The command is: 'cd folder' where 'folder; is the folder to which you want to change. For example, if the folder that was being changed to was entitled 'Secure', then the proper command would be 'cd Secure'. 
  3. Type in the name of the preferred UNIX text editor at the command prompt and press the 'Enter' key.
    NOTE: Use Pico (because it's simple to use) if there is no preferred text editor. 
  4. The following instructions will assume you're using Pico.  The command to open Pico is: 'pico'. The text editor with an empty file open will become visible.  
  5. Hold the 'Control key' and press the 'O' key to save this blank file. 
  6. When asked for a name, type '.htaccess', and press the 'Enter' key.  It will begin your editing session in Pico with a blank file named '.htaccess'.  It is possible to choose what to do from here with the options below.

Require a Visitor to Log In to the Website

It is possible to let certain individuals have access to the site, or certain portions of the site.

  1. On a new (or the first) line of the empty '.htaccess' file, type 'AuthType TLTlogin'.
  2. On the next line (or two lines down if preferred), type 'Require user username username2', where 'username' begins the list of username's that should have access to this site or portion of the site.  For example, if adding the usernames 'jpublic', 'janepub', and 'jonpub', the proper command prompt would be 'Require user jpublic janepub jonpub'.  
    NOTE: Pico does not wrap at the end of lines; rather it starts new lines.  If you include more usernames than will fit on the line, it will be necessary to use a more advanced text editor, such as Emacs or vi.
  3. Hold the 'Control' key and press the 'O' key.  
  4. Press the 'Enter' key to save the changes.  The next time anyone visits the webpage, they will need to log in using their career account credentials.  If you would prefer to assign usernames and passwords, see the advanced topics section at the end of this article.

Changing the Site's 'index' file

If you would like to have an alternate index (home) page, such as index.php, myhome.htm, or any kind of similar file that the server can show as a Web page, then follow the instructions below.

  1. In the '.htaccess' file, type the  'DirectoryIndex indexfilename.ext' and press the 'Enter' key. 
    NOTE: 'indexfilename.ext' is the full name (including the extension) of the file that is intended to be the start page.  It is possible to list multiple options, separated by spaces, but the server reads from left to right and will display the first that exists.
  2. Optionally, if a start page is not currently uploaded, and someone visits the site, a listing of all the files and folders in the website will be visible that they can access or download.  If you want them to load a specific home page, add the following line to the '.htaccess' file: 'Options-Indexes'. 
  3. Hold the 'Control' key and press the 'O' key. 
  4. Press the 'Enter' key to save the changes.  The next time that the site is accessed, the chosen start page will be displayed.

Advanced Topics 

For additional tutorials on '.htaccess' files, see the following websites:

Troubleshooting

If the changes are not visible right away, try refreshing or clearing the browser's temporary page cache.

The first time an '.htaccess' file is created, webfix will need to be executed again. 

 

Still need help?  Click the 'Purdue IT Request' button to start a ticket.

Purdue IT Request Print Article

Details

Article ID: 452
Created
Fri 8/4/23 2:24 PM
Modified
Fri 8/4/23 2:24 PM

Related Services / Offerings (2)

Website hosting and monitoring.
Website Hosting request