Ht Access Tweaks

HT Access Tweaks x

Conditions

  • R $1 with $n (1 <=n <=9) - access temporary values of current RewriteRule directive.
  • R %2 with %n (1 <=n <=9) - access temporary values of previous RewriteCond directive.

Conditions works like a Perl regular expression without delimiter, describing the condition required for string. An exclamation mark before an expression means not.

  • C < - comparison operator. String comes before the string to compare.
  • C > - comparison operator. String comes after the string to compare.
  • C = - comparison operator. String and the string to compare are checked for equality.
  • C -d - String is interpreted as pathname and checked if it is a directory.
  • C -f - String is interpreted as pathname and checked if it is a file.
  • C -s - String is interpreted as pathname and checked if it is a file with a size greater than zero.
  • C -l - String is interpreted as pathname and checked if it is a symbolic link.
  • C -F - String is interpreted as pathname and checked if it is an accessible file.
  • C -U - queries if String is a valid URL.

Flags are optional. You can use them to manipulate the value of RewriteCond. Multiple flags are separated with a comma.

  • NC (no case) - operation is case insensitive.
  • OR (or next) - logical OR linking the current condition to the next. Default is AND.

RewriteEngine

  • RewriteEngine on - activates mod_rewrite.
  • RewriteBase / - sets the base directory.

RewriteLogLevel

The default value of RewriteLogLevel is zero, effectively disabling all logging. A value of one logs important stuff only. Values greater than two are more verbose but may stress the server.

  • RewriteLog "/var/www/mod_rewrite.log"
  • RewriteLogLevel 1

RewriteRule

RewriteRule defines the actual redirect and may occur multiple times. Ordering is important because every redirect is executed in exactly the order it occurs.

Parameters are given in the form of source and target where source is a Perl compatible regular expression without delimiter indicating the redirect source and target is the redirect target if the condition is true. RewriteRule may have additional options, which are executed if the condition is true.

  • R (redirect) - execute an external redirect with HTTP code 302. Different HTTP codes may be added in the form of R=nnn.
  • R (forbidden) - send HTTP code 403 FORBIDDEN header to browser.
  • R (gone) - send HTTP code 410 GONE header to browser.
  • R (last) - indicates not to execute any more directives.
  • R (next) - break current execution and start over. Beware endless loop!
  • R (chain) - indicates to continue with the next rule only if current is true.
  • R (type) - applies a MIME type.
  • R (no sub) - ignore this rule if the current request is a sub request.
  • R (no case) - case insensitive operation.
  • R (qs append) - mandatory if detour has manually attached query string not changing the original.
  • R (no escape) - turn off ESC character masquerading.
  • R (skip) - ignore the following n directives. S=2 - skip the next two directives and continue with the third.
  • R (env=X:Y) - create variable X with a value of Y.
  • R (cookie=X:Y:foo[:lifetime[:path]]) - set cookie X with value Y and foo as domain. Optional arguments are the cookie's lifetime in minutes and the path.
  • R (proxy) - execute the request using mod_proxy.
  • R (pass through) - pass the request through multiple translators.

Enable Gzip Compression

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE
                        text/html
                        text/plain
                        text/xml
                        text/css
                        text/javascript
                        image/vnd.microsoft.icon
</IfModule>
          

Fix Trailing Slash

DirectorySlash On
          

Redirect to HTTPS

RewriteCond %{HTTP_HOST} ^!example.com
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
          

Redirect to non-www

RewriteCond %{HTTP_HOST} !^example.com [NC]
RewriteRule ^(.*) http://example.com/$1 [R=301,L]