Difference between revisions of "Apache httpd/mod rewrite"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Jump to navigation Jump to search
(taking notes in my userspace)
(→‎examples: inside VirtualHost)
Line 20: Line 20:
 
** '''filesystem-path''' (only in VirtualHost)
 
** '''filesystem-path''' (only in VirtualHost)
  
===examples===
+
===manual examples===
(also from the Apache manual)
 
 
 
 
The following apply inside the per-directory configuration* for <code>/somepath</code> for request <code>GET /somepath/localpath/pathinfo</code>
 
The following apply inside the per-directory configuration* for <code>/somepath</code> for request <code>GET /somepath/localpath/pathinfo</code>
 
: (*<code>/physical/path/to/somepath/.htaccess</code>, with <code>RewriteBase "/somepath"</code>)
 
: (*<code>/physical/path/to/somepath/.htaccess</code>, with <code>RewriteBase "/somepath"</code>)
Line 80: Line 78:
 
</tr>
 
</tr>
 
</table>
 
</table>
 +
 +
The following apply inside per-server configuration* for request <code>GET /somepath/pathinfo</code>:
 +
: (*VirtualHost in <code>httpd.conf</code> or presumably <code>/etc/apache2/sites-enabled/sitename.conf</code>)
 +
 +
<table class="bordered"><tr class="header">
 +
<th>Given Rule</th>
 +
<th>Resulting Substitution</th>
 +
</tr>
 +
<tr>
 +
<td>^/somepath(.*) otherpath$1</td>
 +
<td>invalid, not supported</td>
 +
</tr>
 +
<tr class="odd">
 +
<td>^/somepath(.*) otherpath$1  [R]</td>
 +
<td>invalid, not supported</td>
 +
</tr>
 +
<tr>
 +
<td>^/somepath(.*) otherpath$1  [P]</td>
 +
<td>invalid, not supported</td>
 +
</tr>
 +
<tr class="odd">
 +
<td>^/somepath(.*) /otherpath$1</td>
 +
<td>/otherpath/pathinfo</td>
 +
</tr>
 +
<tr>
 +
<td>^/somepath(.*) /otherpath$1 [R]</td>
 +
<td>http://thishost/otherpath/pathinfo via external redirection</td>
 +
</tr>
 +
<tr class="odd">
 +
<td>^/somepath(.*) /otherpath$1 [P]</td>
 +
<td>doesn't make sense, not supported</td>
 +
</tr>
 +
<tr>
 +
<td>^/somepath(.*) http://thishost/otherpath$1</td>
 +
<td>/otherpath/pathinfo</td>
 +
</tr>
 +
<tr class="odd">
 +
<td>^/somepath(.*) http://thishost/otherpath$1 [R]</td>
 +
<td>http://thishost/otherpath/pathinfo via external redirection</td>
 +
</tr>
 +
<tr>
 +
<td>^/somepath(.*) http://thishost/otherpath$1 [P]</td>
 +
<td>doesn't make sense, not supported</td>
 +
</tr>
 +
<tr class="odd">
 +
<td>^/somepath(.*) http://otherhost/otherpath$1</td>
 +
<td>http://otherhost/otherpath/pathinfo via external redirection</td>
 +
</tr>
 +
<tr>
 +
<td>^/somepath(.*) http://otherhost/otherpath$1 [R]</td>
 +
<td>http://otherhost/otherpath/pathinfo via external redirection (the [R] flag is redundant)</td>
 +
</tr>
 +
<tr class="odd">
 +
<td>^/somepath(.*) http://otherhost/otherpath$1 [P]</td>
 +
<td>http://otherhost/otherpath/pathinfo via internal proxy</td>
 +
</tr>
 +
</table>
 +
 
==Notes==
 
==Notes==
 
* Woozle's: [[User:Woozle/mod_rewrite]]
 
* Woozle's: [[User:Woozle/mod_rewrite]]

Revision as of 11:25, 3 March 2017

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)

manual examples

The following apply inside the per-directory configuration* for /somepath for request GET /somepath/localpath/pathinfo

(*/physical/path/to/somepath/.htaccess, with RewriteBase "/somepath")
Given Rule Resulting Substitution
^localpath(.*) otherpath$1 /somepath/otherpath/pathinfo
^localpath(.*) otherpath$1 [R] http://thishost/somepath/otherpath/pathinfo via external redirection
^localpath(.*) otherpath$1 [P] doesn't make sense, not supported
^localpath(.*) /otherpath$1 /otherpath/pathinfo
^localpath(.*) /otherpath$1 [R] http://thishost/otherpath/pathinfo via external redirection
^localpath(.*) /otherpath$1 [P] doesn't make sense, not supported
^localpath(.*) http://thishost/otherpath$1 /otherpath/pathinfo
^localpath(.*) http://thishost/otherpath$1 [R] http://thishost/otherpath/pathinfo via external redirection
^localpath(.*) http://thishost/otherpath$1 [P] doesn't make sense, not supported
^localpath(.*) http://otherhost/otherpath$1 http://otherhost/otherpath/pathinfo via external redirection
^localpath(.*) http://otherhost/otherpath$1 [R] http://otherhost/otherpath/pathinfo via external redirection (the [R] flag is redundant)
^localpath(.*) http://otherhost/otherpath$1 [P] http://otherhost/otherpath/pathinfo via internal proxy

The following apply inside per-server configuration* for request GET /somepath/pathinfo:

(*VirtualHost in httpd.conf or presumably /etc/apache2/sites-enabled/sitename.conf)
Given Rule Resulting Substitution
^/somepath(.*) otherpath$1 invalid, not supported
^/somepath(.*) otherpath$1 [R] invalid, not supported
^/somepath(.*) otherpath$1 [P] invalid, not supported
^/somepath(.*) /otherpath$1 /otherpath/pathinfo
^/somepath(.*) /otherpath$1 [R] http://thishost/otherpath/pathinfo via external redirection
^/somepath(.*) /otherpath$1 [P] doesn't make sense, not supported
^/somepath(.*) http://thishost/otherpath$1 /otherpath/pathinfo
^/somepath(.*) http://thishost/otherpath$1 [R] http://thishost/otherpath/pathinfo via external redirection
^/somepath(.*) http://thishost/otherpath$1 [P] doesn't make sense, not supported
^/somepath(.*) http://otherhost/otherpath$1 http://otherhost/otherpath/pathinfo via external redirection
^/somepath(.*) http://otherhost/otherpath$1 [R] http://otherhost/otherpath/pathinfo via external redirection (the [R] flag is redundant)
^/somepath(.*) http://otherhost/otherpath$1 [P] http://otherhost/otherpath/pathinfo via internal proxy

Notes