Apache httpd/mod rewrite

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< Apache httpd
Revision as of 10:37, 3 March 2017 by Woozle (talk | contribs) (Created page with "==Syntax== The [http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html Apache manual] says: * '''RewriteRule''' {{arg|Pattern}} {{arg|Substitution}} [{{arg|flags}}] * '''Rewri...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Syntax

The Apache manual says:

  • RewriteRule <Pattern> <Substitution> [<flags>]
  • RewriteCond <TestString> <CondPattern> [<flags>]
  • RewriteBase <URL-path>

I think I still don't really understand RewriteBase. Apparently it does need to refer to a path that actually exists, rather than a mapped path. If the path doesn't exist, Apache will log a "core:alert" error saying "RewriteBase: argument is not a valid URL".

RewriteRule

Arguments:

  • Pattern is a perl compatible regular expression, matched against:
    • if VirtualHost context: the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").
    • if per-directory context (Directory and .htaccess): the partial path to the Directory or .htaccess file
      • e.g. a request of "/app1/index.html" may result in comparison against "app1/index.html" or "index.html" depending on where the RewriteRule is defined.
      • The directory path where the rule is defined is stripped from the currently mapped filesystem path before comparison (up to and including a trailing slash). The net result of this per-directory prefix stripping is that rules in this context only match against the portion of the currently mapped filesystem path "below" where the rule is defined.
  • Substitution may be:
    • URL-path: A DocumentRoot-relative path to the intended target
    • Absolute-URL: if the hostname matches the current host, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed.
    • filesystem-path (only in VirtualHost)

Official example:

  • RewriteRule ^(.+) /other/archive/$1 [R]