Systems Status

Announce RSS Feed RSS

Blog RSS Feed RSS

Help Topics

Deschutes Facilities

Campus Facilities

User Account Info

Roundcube Mail

Contact Systems

ipv6 ready

.htaccess Configuration

The .htaccess file allows you to configure how the web server handles pages in your public_html directory. You can use this to restrict access, modify the look of indexes, rewrite URLs, or add the automatic processing of additional file types.

Restricting Access

You can use the .htaccess configuration file to restrict access to your web pages. The .htaccess goes into the base directory of the pages you want to protect.

Restrict Access Based on IP Address

You can restrict access by IP address. This would allow you to restrict access to users within the University or just the CS Department. You can also password protect pages so that users will be prompted to enter a username and password to access your pages.

If you want to restrict access to files in ~/public_html/restricted/ to users in the CS Department, create a .htaccess file that looks like this:

jdash@ix: ~/public_html/restricted 603$ cat .htaccess
require host

Make sure the file has permissions 0755.

Restrict Access by Password

If you want to password protect your pages you'll need an htpasswd file as well as a .htaccess file. Create your htpasswd file with the htpasswd command. In this example the -c creates a new password file. Leave it off to add additional password to an existing password file.

jdash@ix: ~/etc 549$ htpasswd -c htpasswd snoopy
Adding password for snoopy.
New password:
Re-type new password:
jdash@ix: ~/etc 550$

The .htaccess file should look like this. Make sure you replace /home/users/jdash/etc/ with the full path to the directory that contains your htpasswd file and project name is a name that you make up.

AuthUserFile  /home/users/jdash/etc/htpasswd
AuthGroupFile /dev/null
AuthName      "project name"
AuthType      Basic
require valid-user

Adjusting Automatic Indexes

If you are using the built-in auto-indexing features of Apache, you can control this using an .htaccess file. Some of the things you can control are: what files get included, how they are sorted, and header and readme documents.

Suppose you had a directory of documents. You'd like to announce which documents are your favorites, by adding a star icon. You also want all of the files sorted with the newest first. This can be done by creating an .htaccess file containing the following commands (change myfav to your favorite documents):

IndexOrderDefault Descending Date
AddIcon /icons/misc/star.gif myfav

Controlling File Types

and .PHP as a CGI.

Another interesting application of the .htaccess file is to control how different files are processed by the webserver. This allows you to create additional file types that are served in different ways.

NB: You don't need to do this on our server to make PHP execute as a CGI, because we do this for you in our server configuration.

The most common use for this is probably to execute .php files using the CGI interface rather than the built-in mod_php interface. This would only be used if you were installing a bulletin board or content management system that had more .php files than were easy to convert to .cgi, and would allow you to control access to the data files. To do this you would create a .htaccess file (replace /~jdash/cgi.php with your URL) in whichever directory contained the application.

RemoveHandler .php
AddType application/my-httpd-php .php
Action application/my-httpd-php /~jdash/php.cgi

You would also need to create ~jdash/public_html/php.cgi containing the following code. This is used to check that it is being called by the webserver, then to execute your .php file.

$pwuid = posix_getpwuid(posix_geteuid());
if (is_file($_SERVER['PATH_TRANSLATED']) &&
      ($pwuid['name'] === 'nobody' ||
       $pwuid['name'] === 'apache' ||
       fileowner($_SERVER['PATH_TRANSLATED']) == posix_geteuid())) {

Make sure the permissions on both files are 0755. And, that they are owned by you, in your primary group.

Other possible uses for this technology are: convert/resize images before downloading, automatically create html from text pages, or add a counter database for downloading various other data types.

External Links

Edited: April 06, 2016, at 09:48 AM
Copyright © 2016, University of Oregon, All rights reserved
Privacy Policy