Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision |
htaccess_recipes [2021/04/05 07:13] – [Specify a default character encoding] hc9 | htaccess_recipes [2022/02/13 20:59] – changed to internal DokuWiki link jquah |
---|
====== .htaccess recipes ====== | ====== .htaccess recipes ====== |
| |
| === 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 instance, 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. | .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 instance, 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. |
| |
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 generalize it). If you need some help, jump on com or post your request on bboard. | 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 generalize it). If you need some help, jump on com or post your request on bboard. |
| |
| |
====== Recipes ====== | ====== Recipes ====== |
===== Redirect to a custom error page ===== | ===== 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 | 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: [[custom_error_pages_for_your_site|custom error pages for your site]] |
| |
===== Deny directory listing ===== | ===== Deny directory listing ===== |
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). | 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 ==== | ===== Password protect your directories ===== |
| |
This is a FAQ: http://sdf.org/index.cgi?faq?WEB?04 | This is a FAQ: http://sdf.org/index.cgi?faq?WEB?04 |
| |
==== Force visitors to use SSL/HTTPS ==== | ===== 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: | 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: |