Dump of existing HTML tutorials into docuwiki format by the HTML::WikiConverter::DokuWiki Perl module. Useful for moving things from HTML tutes to the wiki at the SDF wiki. Convert script and source HTML also provided.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
9.3 KiB

====== .htaccess recipes ======
- [[#intro|Introduction]]
- [[#recipes|Recipes]]
- [[#redir-error|Redirect to a custom error page]]
- [[#deny-dir|Deny directory listing]]
- [[#addtype|Add (or force) MIME-type]]
- [[#no-ext|Access files without specifying the extension
* [[#add-ext|I want to access files without extension, but my (cgi|pl|php) is not found]]]]
- [[#remove-handler|Serve .pl .php .cgi etc. as plain text files]]
- [[#download|Force a download with a specific filename]]
- [[#charset|Specify a default character encoding]]
- [[#password|Password protect your directories]]
- [[#forcessl|Force visitors to use SSL/HTTPS]]
===== Introduction =====
.htaccess is the default file used by the [[http://httpd.apache.org/|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 [[http://httpd.apache.org/docs/2.2/mod/core.html#forcetype|ForceType]] directive, but it's not for the [[http://httpd.apache.org/docs/2.2/mod/core.html#errorlog|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:
* [[http://httpd.apache.org/docs/2.2/howto/htaccess.html|Apache Tutorial: .htaccess files]]
* [[http://wiki.apache.org/httpd/Htaccess|Httpd Wiki > Htaccess]]
* [[http://www.htaccess-guide.com/|htaccess-guide.com]]
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.
===== Recipes =====
==== Redirect to a custom error page ====
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
==== Deny directory listing ====
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, [[http://freeshell.org/index.cgi?faq?WEB?04|password protect them]]). Add this to your .htaccess file:
" Options -Indexes "
==== Add (or force) MIME-type ====
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 [[http://en.wikipedia.org/wiki/Cabinet_(file_format)|.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 [[http://en.wikipedia.org/wiki/Internet_media_type#List_of_common_media_types|wikipedia]] or read a full list on [[http://www.iana.org/assignments/media-types/|IANA's website]].
==== Access files without specifying the extension ====
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 "
=== I want to access files without extension, but my (cgi|pl|php) is not found ===
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 "
==== Serve .pl .php .cgi etc. as plain text files ====
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 "
==== Force a download with a specific filename ====
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> "
==== Specify a default character encoding ====
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 [[http://www.w3.org/International/questions/qa-when-lang-neg|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).
==== Password protect your directories ====
This is a FAQ: http://sdf.org/index.cgi?faq?WEB?04
==== [[|Force visitors to use SSL/HTTPS]] ====
As SDF expands its support of [[https://letsencrypt.org/|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.
$Id: htaccess.html,v 1.3 2018/07/30 15:30:01 dave Exp $