Harvard University
Division of Continuing Education
Extension School
Course Web Site: http://www.courses.fas.harvard.edu/~cscie12/
Copyright © 1998-2001 David P. Heitmeyer
httpd.conf
).htaccess
).htaccess
files. .htaccess
files is read on a per request basis..htaccess
/home/c/s/cscie12/public_html/apache/example/.htaccess
ErrorDocument 404 /~cscie12/status404.html
status404.html
/home/c/s/cscie12/public_html/status404.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML lang="en"> <HEAD> <TITLE> CSCIE12: 404 Not Found </TITLE> <BASE href="http://www.courses.fas.harvard.edu/~cscie12/"> </HEAD> <BODY bgcolor="#ffffff" link="#cc3333" vlink="#996633" background="images/background.gif"> <H1>404 Not Found</H1> <H2>CSCIE12: Introduction to Web Site Development</H2> The resource you requested, <br> <strong><!--#echo var="REQUEST_URI"--></strong><br> cannot be found. <HR> The main areas of the site are:<p> <!--#include virtual="inc/nav.html"--> <HR> <!--#include virtual="inc/footer.html"--> <HR> </BODY> </HTML>
.htaccess
files apply to the directory that contains the .htaccess
file and all its descendants.
Directives within the file,
/home/c/s/cscie12/public_html/.htaccess
would apply to all files within and "under" the public_html directory for the user cscie12.
Directives within the file,
/home/c/s/cscie12/public_html/assignments/.htaccess
would apply to all files within and "under" the public_html/assignments directory for the user cscie12.
.htaccess
file, the most likely cause of the problem is incorrect permissions and/or an error in the directive syntax.
.htaccess
file are not set correctly. Just like HTML and image files, the server must be able to read the .htaccess
file. The simplest way to allow that is to make your .htaccess
file readable by "other".
fas% pwd
/home/j/h/jharvard/public_html
is03:~% ls -l .htaccess
-rw------- 1 jharvard founder 349 Nov 27 00:03 .htaccess
is03:~% chmod o+r .htaccess
is03:~% ls -l ~/public_html/.htaccess
-rw----r-- 1 jharvard founder 349 Nov 27 00:03 .htaccess
.htaccess
file will result in a 500 Internal Server Error. In addition, correct usage of a directive that is not allowed in the .htaccess
file will result in a 500 status code. Whether or not a directive is allowed depends upon the server configuration file (httpd.conf; AllowOverride) and the directive itself..htaccess
files. For example, the file corresponding to the URL, http://www.courses.fas.harvard.edu/~cscie12/.htaccess exists and is readable by the Web server, but if we try to follow the link, we get a 403 Forbidden response.ls
command will not list files or directories that begin with a '.' (dot). In order to see the .htaccess
file when you do a directory listing, use the -a (all) option:fas% ls assignments cgi-bin faq images inc index.html instructors lecture schedule.html section syllabus fas% ls -a . .. .htaccess assignments cgi-bin faq images inc index.html instructors lecture schedule.html section syllabus
Files
and FilesMatch
can be used within .htaccess
files.
Examples:
<Files .htaccess> Order allow,deny Deny from all </Files>
# deny access to any tilde backup files <Files *~> Order allow,deny Deny from all </Files>
.htaccess
file:
ErrorDocument 404 /~cscie12/status404.html
Redirecting client requests can be very useful:
http-equiv
attribute of the <META>
element. More information and examples are provided at http://www.fas.harvard.edu/~web/tutorial/meta/refresh/. The recommended method is to do it at the server level.
.htaccess
file:
Redirect 302 /~cscie12/dce.html http://www.dce.harvard.edu/ Redirect 301 /~cscie12/presentation http://www.courses.fas.harvard.edu/~cscie12/lecture
http://www.courses.harvard.edu/<4 digit catalog number>
RewriteEngine On RewriteBase /~cscie12 RewriteCond %{HTTP_USER_AGENT} ^Lynx RewriteRule ^(index.html)?$ text/ [R=302]
fas% lwp-request -USed -H"User-Agent: Lynx" \ http://www.courses.fas.harvard.edu/~cscie12/index.html GET http://www.courses.fas.harvard.edu/~cscie12/text/ User-Agent: Lynx GET http://www.courses.fas.harvard.edu/~cscie12/index.html --> 302 Found GET http://www.courses.fas.harvard.edu/~cscie12/text/ --> 200 OK Connection: close Date: Mon, 27 Nov 2001 19:47:04 GMT Server: Apache/1.3.12 (Unix) secured_by_Raven/1.4.3 mod_perl/1.24 Content-Type: text/html Author: David P. Heitmeyer Client-Date: Mon, 27 Nov 2001 19:47:04 GMT Client-Peer: 140.247.30.64:80 Title: Fundamentals of Web Site Development X-Meta-Generator: HTML Tidy, see www.w3.org
~cscie12/public_html/index2.html
<link title="Text-only version" rel="alternate" href="http://www.courses.fas.harvard.edu/text/index.html" media="aural, braille, tty">
index2.html
provides the text-only version as a link:
Fundamentals of Web Site Development (p1 of 2) #Text-only version Harvard University, DCE Fall 2001 CSCIE12 Fundamentals of Web Site Development David P. Heitmeyer _______________________________________________________________ Week of November 20 * Lecture 9 Handout: HTTP * Lecture 8 Video and Handouts: JavaScript, Usability, Accessibility, Other Content-Types * Assignment 4 and Submission Form Available., Due Wednesday, November 15, 2001 * Lecture 7 Video and Handouts: CSS and JavaScript * Lecture 6 Video and Handouts: Web Site Architecture and Design; special guest lecture by Elaine Benfatto, Harvard University -- press space for next page -- Arrow keys: Up and Down to move. Right to follow a link; Left to go back. H)elp O)ptions P)rint G)o M)ain screen Q)uit /=search [delete]=history list |
rwx-----x
and rwx---r-x
permissions?
main.html
or overview.html
to be the default files returned when a directory is requested?DirectoryIndex index.html main.html overview.html slide1.html
fas% pwd /home10/c/s/cscie12/public_html/lecture/autoindex/ex2 fas% ls -la total 28 drwxr-xr-x 2 cscie12 courses 8192 Nov 27 13:28 . drwxr-xr-x 6 cscie12 courses 8192 Nov 27 13:11 .. -rw-r--r-- 1 cscie12 courses 207 Nov 27 13:12 .htaccess -rw-r--r-- 1 cscie12 courses 147 Nov 27 13:09 HEADER.html -rw-r--r-- 1 cscie12 courses 66 Nov 27 13:09 README.html -rw-r--r-- 1 cscie12 courses 4168 Nov 27 12:58 client-server.gif -rw-r--r-- 1 cscie12 courses 906 Nov 27 12:58 slide1.html -rw-r--r-- 1 cscie12 courses 743 Nov 27 12:58 slide2.html -rw-r--r-- 1 cscie12 courses 1208 Nov 27 12:58 slide3.html fas% cat .htaccess IndexOptions FancyIndexing IndexOptions IconsAreLinks IconHeight=22 IconWidth=20 \ NameWidth=* ScanHTMLTitles SuppressLastModified \ SuppressSize SuppressColumnSorting \ SuppressHTMLPreamble IndexIgnore *.gif .. fas%
.htaccess
file:
ExpiresActive On ExpiresByType text/html A3600 # HTML expires in 1 hour ExpiresByType image/gif A2592000 # GIF expires in 30 days ExpiresByType image/jpeg A2592000 # JPEG expires in 30 days ExpiresByType image/png A2592000 # PNG expires in 30 days ExpiresDefault "now plus 1 day" # types not specified # expires in 1 day
ExpiresActive On ExpiresByType text/html M86400 # HTML expires 1 day after it was last modified ExpiresDefault M86400
This module controls the setting of the The |
Header set Author "David P. Heitmeyer"
fas% ls -l sendasiam.html.asis -rw----r-- 1 cscie12 courses 344 Nov 28 23:25 sendasiam.html.asis
sendasiam.html.asis
file:
Status: 301 Now where did I leave that URL Location: http://www.joe.com/ Content-type: text/html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN"> <HTML> <HEAD> <TITLE>Lame excuses'R'us</TITLE> </HEAD> <BODY> <H1>Fred's exceptionally wonderful page has moved to <A HREF="http://www.joe.com/">Joe's</A> site. </H1> </BODY> </HTML>
Basic Authentication is analogous to simply "closing a door" to parts of your Web site. It will prevent the casual or polite users from "opening the door", but will not prevent someone mildly determined to walking in.
Two issues that contribute to the lack of security and privacy are:
fas% telnet 140.247.30.64 80 Trying 140.247.30.64... Connected to 140.247.30.64. Escape character is '^]'. HEAD /~cscie12/assignments/ HTTP/1.1 Host: www.courses.fas.harvard.edu HTTP/1.1 401 Authorization Required Date: Mon, 22 Nov 1999 17:42:29 GMT Server: Apache/1.3.6 (Unix) mod_perl/1.21 secured_by_Raven/1.4.1 WWW-Authenticate: Basic realm="CSCIE12 Assignment Submission"
fas% telnet 140.247.30.64 80
Trying 140.247.30.64...
Connected to 140.247.30.64.
Escape character is '^]'.
HEAD /~cscie12/assignments/ HTTP/1.1
Host: www.courses.fas.harvard.edu
Authorization: BASIC Z3Vlc3Q6a25vY2trbm9jaw==
HTTP/1.1 200 OK
Date: Mon, 22 Nov 1999 17:46:42 GMT
Server: Apache/1.3.6 (Unix) mod_perl/1.21 secured_by_Raven/1.4.1
Author: David P. Heitmeyer
Content-Type: text/html
htpasswd
program.htaccess
filehtpasswd
filehtgroup
fileFor example, /home/j/h/jharvard/public_html/private/.htaccess will apply to all files contained within the ~jharvard/public_html/private directory (and its subdirectories), but would not be applied to the file ~jharvard/public_html/index.html.
This file needs to be readable by the Web Server.
This file should not lie within your public_html. It should reside at the root level of your home directory (for example, /home/j/h/jharvard/.htpasswd
This file needs to be readable by the Web Server.
fas% which htpasswd /usr/local/bin/htpasswd fas% htpasswd Usage: htpasswd [-c] passwordfile username The -c flag creates a new file.
fas% more ~cscie12/.htpasswd.demo guest:79WeSn3vYGsKQ guest2:wGcgIYLtHNIpM guest3:j9VzpSX/C8Kr2 guest4:CjHmW1PWNFwXM
This file should not lie within your public_html. It should reside at the root level of your home directory (for example, /home/j/h/jharvard/.htgroup
This file needs to be readable by the Web Server.
The following .htpasswd.demo and .htgroup.demo files are used:
/home/c/s/cscie12/.htpasswd.demoice% htpasswd Usage: htpasswd [-c] passwordfile username The -c flag creates a new file. ice% htpasswd -c /home/c/s/cscie12/.htpasswd.demo guest Adding password for guest New password: ***** Re-type password: *****
Contents of file:
guest:79WeSn3vYGsKQ guest2:PR4APgA.4CKO. guest3:5DbCMPbSDstj2 guest4:htPnr8jT4bI5E
.htgroup.demo
Contents of file:
VIP: guest guest4
The"AuthName" is the description that is displayed by the browser in the Basic Authentication dialog box.
Contents of sample .htaccess file:AuthName "Basic Authentication Tutorial 1" AuthType Basic AuthUserFile /home/c/s/cscie12/.htpasswd.demo require valid-user
fas% lwp-request -USed -Cguest2:iforgot \ http://www.courses.fas.harvard.edu/~cscie12/apache/access/example1 GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example1 Authorization: Basic Z3Vlc3QyOmlmb3Jnb3Q= User-Agent: lwp-request/1.38 GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example1 --> 401 Authorization Required GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example1 --> 401 Authorization Required Connection: close Date: Mon, 27 Nov 2001 19:13:51 GMT Server: Apache/1.3.12 (Unix) secured_by_Raven/1.4.3 mod_perl/1.24 WWW-Authenticate: Basic realm="Basic Authentication Tutorial 1" Content-Base: http://www.courses.fas.harvard.edu/~cscie12/ Content-Type: text/html Author: David P. Heitmeyer Client-Date: Mon, 27 Nov 2001 19:13:51 GMT Client-Peer: 140.247.30.64:80 Client-Warning: Credentials for 'guest2' failed before Title: CSCIE12: 401 Unauthorized fas% lwp-request -USed -Cguest2:guest \ http://www.courses.fas.harvard.edu/~cscie12/apache/access/example1 GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example1/ Authorization: Basic Z3Vlc3QyOmd1ZXN0 User-Agent: lwp-request/1.38 GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example1 --> 401 Authorization Required GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example1 --> 301 Moved Permanently GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example1/ --> 200 OK Connection: close Date: Mon, 27 Nov 2001 19:12:58 GMT Server: Apache/1.3.12 (Unix) secured_by_Raven/1.4.3 mod_perl/1.24 Content-Type: text/html Author: David P. Heitmeyer Client-Date: Mon, 27 Nov 2001 19:12:58 GMT Client-Peer: 140.247.30.64:80 Title:
Contents of sample .htaccess file:
AuthName "Basic Authentication Tutorial 2" AuthType Basic AuthUserFile /home/c/s/cscie12/.htpasswd.demo require user guest2 guest3
Unauthorized:
guest:guest
guest4:guest
fas% lwp-request -USed -Cguest2:guest \ http://www.courses.fas.harvard.edu/~cscie12/apache/access/example2/ GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example2/ Authorization: Basic Z3Vlc3QyOmd1ZXN0 User-Agent: lwp-request/1.38 GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example2/ --> 401 Authorization Required GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example2/ --> 200 OK Connection: close Date: Mon, 27 Nov 2001 19:15:11 GMT Server: Apache/1.3.12 (Unix) secured_by_Raven/1.4.3 mod_perl/1.24 Content-Type: text/html Author: David P. Heitmeyer Client-Date: Mon, 27 Nov 2001 19:15:11 GMT Client-Peer: 140.247.30.64:80 Title: fas% lwp-request -USed -Cguest4:guest \ http://www.courses.fas.harvard.edu/~cscie12/apache/access/example2/ GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example2/ Authorization: Basic Z3Vlc3Q0Omd1ZXN0 User-Agent: lwp-request/1.38 GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example2/ --> 401 Authorization Required GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example2/ --> 401 Authorization Required Connection: close Date: Mon, 27 Nov 2001 19:15:22 GMT Server: Apache/1.3.12 (Unix) secured_by_Raven/1.4.3 mod_perl/1.24 WWW-Authenticate: Basic realm="Basic Authentication Tutorial 2" Content-Base: http://www.courses.fas.harvard.edu/~cscie12/ Content-Type: text/html Author: David P. Heitmeyer Client-Date: Mon, 27 Nov 2001 19:15:22 GMT Client-Peer: 140.247.30.64:80 Client-Warning: Credentials for 'guest4' failed before Title: CSCIE12: 401 Unauthorized
Contents of .htaccess file:
AuthName "Basic Authentication Tutorial 3" AuthType Basic AuthUserFile /home/c/s/cscie12/.htpasswd.demo AuthGroupFile /home/c/s/cscie12/.htgroup.demo require group VIP
Contents of .htgroup.demo file:
VIP: guest guest4
Unauthorized:
guest2:guest
guest3:guest
Contents of sample .htaccess file:
order deny,allow deny from all allow from 140.247 allow from 128.103 allow .harvard.edu
fas% lwp-request -USed \ http://www.courses.fas.harvard.edu/~cscie12/apache/access/example4/ GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example4/ User-Agent: lwp-request/1.38 GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example4/ --> 200 OK Connection: close Date: Mon, 27 Nov 2001 19:17:01 GMT Server: Apache/1.3.12 (Unix) secured_by_Raven/1.4.3 mod_perl/1.24 Content-Type: text/html Author: David P. Heitmeyer Client-Date: Mon, 27 Nov 2001 19:17:01 GMT Client-Peer: 140.247.30.64:80 Title:
Contents of sample .htaccess file:
order allow,deny allow from all deny from .fas.harvard.edu
fas% lwp-request -USed \ http://www.courses.fas.harvard.edu/~cscie12/apache/access/example5/ GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example5/ User-Agent: lwp-request/1.38 GET http://www.courses.fas.harvard.edu/~cscie12/apache/access/example5/ --> 403 Forbidden Connection: close Date: Mon, 27 Nov 2001 19:17:53 GMT Server: Apache/1.3.12 (Unix) secured_by_Raven/1.4.3 mod_perl/1.24 Content-Base: http://www.courses.fas.harvard.edu/~cscie12/ Content-Type: text/html Author: David P. Heitmeyer Client-Date: Mon, 27 Nov 2001 19:17:53 GMT Client-Peer: 140.247.30.64:80 Title: CSCIE12: 403 Forbidden
Contents of sample .htaccess file:
order deny,allow deny from all allow from .yale.edu AuthType Basic AuthUserFile /home/c/s/cscie12/.htpasswd.demo AuthName "Basic Authentication Tutorial 6" require valid-user satisfy any
Contents of sample .htaccess file:
order deny,allow deny from all allow from .harvard.edu AuthType Basic AuthUserFile /home/c/s/cscie12/.htpasswd.demo AuthName "Basic Authentication Tutorial 7" require valid-user satisfy all
Contents of sample .htaccess file:
SSLRequireSSL
.htaccess
files. Some are not.
ErrorDocument directiveSyntax: ErrorDocument error-code documentContext: server config, virtual host, directory, .htaccess Status: core Override: FileInfo Compatibility: The directory and .htaccess contexts are only available in Apache 1.1 and later. |
Also, the "a" indicator on the Apache Quick Reference Card indicates that the directive is valid within an .htaccess
file.
AllowOverride
directive that determines which directives within .htaccess
files Apache will process. The Override line that is given for each directive in the Apache documentation indicates which configuration directive must be active in order to use that directive with an .htaccess
file.
For the FAS system, the main server configuration file has the following directive in place for users' public_html directories:
AllowOverride FileInfo AuthConfig Limit Indexes Options
ErrorDocument directiveSyntax: ErrorDocument error-code documentContext: server config, virtual host, directory, .htaccess Status: core Override: FileInfo Compatibility: The directory and .htaccess contexts are only available in Apache 1.1 and later. |
For example, if the Rewrite Module (mod_rewrite
) has not been activated, none of the Rewrite directives will be available to use.
Refer to the Status and Module lines in the documentation for each directive and to the documentation for the specific Apache installation you are using.
mod_access mod_actions mod_alias mod_asis mod_auth mod_auth_dbm mod_autoindex mod_cgi mod_dir mod_env mod_expires mod_headers mod_imap mod_include mod_log_config mod_mime mod_negotiation mod_perl mod_rewrite mod_setenvif mod_so mod_status mod_unique_id mod_userdir mod_usertrack raven_ssl