htaccess_recipes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
htaccess_recipes [2021/04/05 07:12] – [Serve .pl .php .cgi etc. as plain text files] hc9 | htaccess_recipes [2022/02/14 12:47] – [Save the current directory as an environment variable] jquah | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== .htaccess recipes ====== | ====== .htaccess recipes ====== | ||
+ | |||
+ | === Introduction === | ||
.htaccess is the default file used by the [[http:// | .htaccess is the default file used by the [[http:// | ||
Line 14: | Line 16: | ||
OK, let's see some recipes. The URL http:// | OK, let's see some recipes. The URL http:// | ||
+ | |||
====== Recipes ====== | ====== Recipes ====== | ||
Line 19: | Line 22: | ||
===== 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' | + | 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 ===== | ===== Deny directory listing ===== | ||
Line 27: | Line 30: | ||
'' | '' | ||
+ | ===== Save the current directory as an environment variable ===== | ||
+ | |||
+ | You might already be aware that the options in an htaccess file will affect all the files in subdirectories of the htaccess file location, unless overridden by htaccess files deeper into the path. But did you ever wish you could dynamically infer the location of your base htaccess file, for substitution into later directives? It turns out that Perl regexes (used in Apache' | ||
+ | |||
+ | If you made your website accessible via tilde-style URLs (running '' | ||
+ | |||
+ | < | ||
+ | RewriteEngine on | ||
+ | RewriteBase "/" | ||
+ | RewriteCond $0# | ||
+ | RewriteRule ^.*$ - [E=CWD:%2] | ||
+ | |||
+ | RewriteCond %{HTTP_HOST} ^(www\.)? | ||
+ | RewriteCond %{REQUEST_FILENAME} !-f | ||
+ | RewriteCond %{REQUEST_FILENAME} !-d | ||
+ | RewriteRule ^.*$ %{ENV: | ||
+ | </ | ||
+ | |||
+ | The # symbol serves as an arbitrary delimiter, ensuring that the second parenthesized group captures the working directory that's known to the server at the time of processing. Whether the working directory is an absolute pathname on the filesystem or an alias that will later be expanded by mod_userdir, | ||
+ | |||
+ | This example can be extended to perform different redirections depending on which virtual host handled the transaction (just create a new stanza with a different condition on %{HTTP_HOST}). In this way you can give your visitors the same experience whether they' | ||
+ | |||
+ | Because these RewriteRules impose additional processing demands on the server for each request, hacks like these should be a last resort. If the other workarounds mentioned in [[custom_error_pages_for_your_site|custom error pages for your site]] address your use case, they should be implemented instead. | ||
===== Add (or force) MIME-type ===== | ===== Add (or force) MIME-type ===== | ||
Line 65: | Line 91: | ||
'' | '' | ||
- | ==== Force a download with a specific filename ==== | + | ===== 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: | 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: | ||
Line 71: | Line 97: | ||
''< | ''< | ||
- | ==== Specify a default character encoding ==== | + | ===== Specify a default character encoding |
If you want all your html documents to be served with UTF-8 as the default encoding (or charset): | If you want all your html documents to be served with UTF-8 as the default encoding (or charset): | ||
Line 85: | Line 111: | ||
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:// | This is a FAQ: http:// | ||
- | ==== Force visitors to use SSL/HTTPS ==== | + | ===== Force visitors to use SSL/ |
As SDF expands its support of [[https:// | As SDF expands its support of [[https:// |
htaccess_recipes.txt · Last modified: 2022/08/01 20:35 by peteyboy