This shows you the differences between two versions of the page.
| tutorials:htaccess [2011/04/30 13:45] – created clemens | tutorials:htaccess [2011/10/04 19:11] (current) – Fix formatting: ToC, readability of codelines. memnon | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== .htaccess recipes ====== | ||
| + | |||
| + | Contents | ||
| + | |||
| + | - [[# | ||
| + | - [[# | ||
| + | - [[#Redirect to a custom error page]] | ||
| + | - [[#Deny directory listing]] | ||
| + | - [[#Add (or force) MIME-type]] | ||
| + | - [[#Access files without specifying the extension | ||
| + | * [[#I want to access files without extension, but my (cgi|pl|php) is not found]]]] | ||
| + | - [[#Serve .pl .php .cgi etc. as plain text files]] | ||
| + | - [[#Force a download with a specific filename]] | ||
| + | - [[#Specify a default character encoding]] | ||
| + | - [[#Password protect your directories]] | ||
| + | |||
| + | ===== Introduction ===== | ||
| + | |||
| + | .htaccess is the default file used by the [[http:// | ||
| + | |||
| + | 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, | ||
| + | |||
| + | Remember that .htaccess files must be readable by the server, so you can "chmod 640 .htaccess" | ||
| + | |||
| + | Additional information about .htaccess files can be found in: | ||
| + | |||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | |||
| + | OK, let's see some recipes. The URL http:// | ||
| + | |||
| + | ===== 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' | ||
| + | |||
| + | ==== Deny directory listing ==== | ||
| + | |||
| + | If you type http:// | ||
| + | |||
| + | 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:// | ||
| + | |||
| + | AddType application/ | ||
| + | |||
| + | AddType is the directive, application/ | ||
| + | |||
| + | 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/ | ||
| + | |||
| + | AddType application/ | ||
| + | |||
| + | You can look for common MIME-type on [[http:// | ||
| + | |||
| + | ==== 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:// | ||
| + | |||
| + | 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:// | ||
| + | |||
| + | AddType application/ | ||
| + | |||
| + | If you have perl and/or php files, add (modify the extension as needed): | ||
| + | |||
| + | AddType application/ | ||
| + | |||
| + | ==== 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/ | ||
| + | |||
| + | 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 " | ||
| + | |||
| + | ==== 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, "// | ||
| + | |||
| + | 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:// | ||
| + | |||
| + | 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:// | ||