.htaccess
is the default file used by the Apache HTTP server (and
others) in order to allow dynamic configuration. It's a plain text file that
uses the same syntax present in the main configuration files (e.g.,
httpd.conf). It can contain a subset of Apache directives. The size of this
subset depends on wheter the directives can be overridden or not (and this
is present in the server configuration). In the Apache documentation you can
see if a directive can be placed in a .htaccess file by checking that in the
Context:
line appears .htaccess
. For instace, it's possible
for the ForceType
directive, but it's not for the
ErrorLog
directive.
file.
The configuration directives placed in a .htaccess file will take effect
immediately when a document, located in the directory where the .htaccess
file is located and all subdirectories, is accessed. The server will also
search for .htaccess files in all the parent directories. If there is a
conflicting configuration directive, the server will apply the one that is
in the .htaccess file closer to the requested resource. For instance, suppose
that X
and Y
are two generic options. If you have
Options +X -Y
in $HOME/html/.htaccess and
Options -X
in $HOME/html/files/test/.htaccess, when
you access a file in http://YOURUSERNAME.freeshell.org/files/test/ (and all
subdirectories, unless you have another .htaccess file that reverts the
configuration) options X
and Y
will be disabled, but if you
access a file in http://YOURUSERNAME.freeshell.org/files/ (and above) option
X
will be enabled and option Y
disabled.
Remember that .htaccess files must be readable by the server, so you can
chmod 640 .htaccess
in order to give it the correct
permissions. It's, however, a good practice to run mkhomepg -p
in your SDF shell everytime you play with files in your html
directory.
Additional information about .htaccess files can be found in:
OK, let's see some recipes. The URL http://YOURUSERNAME.freeshell.org/ will be used in the examples, so modify it to suit your needs and remember that your .htaccess file will be placed in $HOME/html/ or in directories under it. Examples solve a specific issue, but they can give you an idea on how to deal with something more generic (i.e., an example could be referred to .pl files, but with a search of the mentioned directives you could generalise it). If you need some help, jump on com or post your request on bboard.
Do you want your visitors see your custom error pages when something goes
wrong (e.g., a page not found
error)? There's already a tutorial
about it: http://sdf.org/index.cgi?tutorials/errorpage
If you type http://YOURUSERNAME.freeshell.org/pics/ you will see a list
of the files present in pics
. Probably you don't want this (if
you don't want that other people see your private stuff, don't put it
on-line, or, at least,
password protect
them). Add this to your .htaccess file:
Options -Indexes
The server could not be aware of all kind of files out there, so will have some troubles trying to figure out what to do with an unknown extension. You can tell the server what to do with unknown file types. Say you have a .cab file. Apache will communicate to your user agent the correct information about the file with:
AddType application/vnd.ms-cab-compressed .cab
AddType
is the directive, application/vnd.ms-cab-compressed
is
the MIME-type present in the Content-Type:
entry in the HTTP headers
sent by Apache, and .cab
is the extension.
Even if the server knows what's the MIME-type of a specific file extension,
you could prefer it to use another one. Let's say that you want .html
files to be served as application/xhtml+xml
(because you are
hardcore). Try this:
AddType application/xhtml+xml .html
You can look for common
MIME-type on
wikipedia or read a full list on
IANA's website.
It could be desiderable to avoid specifying extensions for your html pages. Why? Suppose you've always used http://YOURUSERNAME.freeshell.org/contact.php in your .sig and, at some point, you decide that you want to use perl, so that the new address is http://YOURUSERNAME.freeshell.org/contact.pl. Unless you take other actions (redirection) people that go to the old address will find a 404 page. It would be better to use http://YOURUSERNAME.freeshell.org/contact so that you can go crazy and rewrite your site with all known languages as frequently as you want.
You can use URIs without extensions with:
Options +MultiViews
Suppose that you have a cgi file called script.cgi
and that, once
you enable MultiViews
(see above), when you access
http://YOURUSERNAME.freeshell.org/script you get a 404 page. It's likely
that the server have some problems in determining the MIME-type. In this
case, put in your .htaccess file:
AddType application/x-httpd-cgi .cgi
If you have perl and/or php files, add (modify the extension as needed):
AddType application/x-httpd-php .php
AddType application/x-httpd-perl .pl
If you want the server to execute your files, in order to be able to read the code of some specific files, you can remove the handlers. Let's say that the code you want to read is located in $HOME/html/code/. Now, you can put in $HOME/html/code/.htaccess the following bits:
RemoveHandler .pl .php .cgi
Let's say that you have a pdf file with an unintuitive name
aaa222.pdf
. You might want to force a download when people
access the file and, in doing so, specify a default file name for the file
that will be saved. This will do the job:
<Files x.cab>
Header set Content-Disposition "attachment; filename=Thesis.pdf"
</Files>
If you want all your html documents to be served with UTF-8 as the default
encoding (or charset
):
AddCharset UTF-8 .html
UTF-8
was used as an example, but you can use whatever encoding is
appropriate. Note also that in the example only files with extension
html
will have a default encoding. If you want to extend that
behaviour to other file extensions, add them on the same line. For instance,
AddCharset UTF-8 .html .htm .txt
.
This can also be useful if you want that only pages written in a specific
language are served with a default encoding, while the others use the
encoding sent normally by the server. So, suppose that you're usin language negotiation and have resources in two languages,
english (with extension en.html
) and chinese (with extension
cn.html
). With the following line:
AddCharset UTF-8 .cn
only .cn.html
files will have UTF-8 as the default encoding. (The
order of the language in the extension is not relevant, i.e., the files
could have been html.en
and html.cn
; also, the leading dot in
the extension in the .htaccess file is optional).
This is a FAQ: http://sdf.org/index.cgi?faq?WEB?04
As SDF expands its support of Let's Encrypt, offering free SSL certificates, you may wish to require all visitors of your site to use HTTPS. (This also may improve your search engine ranking, and many Web browsers will soon flag non-SSL sites as "Not Secure.") Adding this to the .htaccess file in your site's root directory will redirect your non-HTTPS visitors accordingly:
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If this suddenly renders your whole site inaccessible, be sure the
permissions on the .htaccess file itself are suitable (chmod 644, or run
mkhomepg -p
) -- the default umask will not allow the Web
server itself to read your .htaccess file.