User Tools

Site Tools


htaccess_recipes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
htaccess_recipes [2022/02/13 20:59] – changed to internal DokuWiki link jquahhtaccess_recipes [2022/02/14 02:52] – Added an example of saving a pattern match to an environment variable jquah
Line 29: Line 29:
  
 ''Options -Indexes'' ''Options -Indexes''
 +
 +===== Save the current directory as an environment variable =====
 +
 +If you made your website accessible via tilde-style URLs (running ''mkhomepg -d'' at the shell), then requests to anything under http://sdf.org/~YOURUSERNAME will have a different setting for DOCUMENT_ROOT than requests to the same content at http://YOURUSERNAME.sdf.org (assuming that ~/html and ~/public_html are symlinks to the same folder). A single htaccess serving both types of URLs cannot blindly make relative path substitutions in a RewriteRule directive (or even well-intentioned absolute path substitutions based on your knowledge of the SDF filesystem). Instead, you can first pass the requested URL through a do-nothing RewriteRule that extracts the current directory into the environment variable ''CWD'', and then use the value of ''CWD'' in a subsequent RewriteRule (which performs the actual substitution). Here is an example, which can be extended to perform different redirections depending on which virtual host handled the transaction. 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, the environment variable ''CWD'' will capture a usable value for the subsequent RewriteRule.
 +
 +<code>
 +RewriteEngine on
 +RewriteBase "/"
 +RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
 +RewriteRule ^.*$ - [E=CWD:%2]
 +
 +RewriteCond %{HTTP_HOST} ^(www\.)?sdf\.org$
 +RewriteCond %{REQUEST_FILENAME} !-f
 +RewriteCond %{REQUEST_FILENAME} !-d
 +RewriteRule ^.*$ %{ENV:CWD}/custom404.html
 +</code>
  
 ===== Add (or force) MIME-type ===== ===== Add (or force) MIME-type =====
htaccess_recipes.txt · Last modified: 2022/08/01 20:35 by peteyboy