Skip to content

Commit

Permalink
mod_alias: Add AliasPreservePath directive to map the full
Browse files Browse the repository at this point in the history
path after the alias in a location.


git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1911067 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
minfrin committed Jul 17, 2023
1 parent 11f9d6c commit bc8431b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changes-entries/alias-preserve-path.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*) mod_alias: Add AliasPreservePath directive to map the full
path after the alias in a location. [Graham Leggett]

43 changes: 43 additions & 0 deletions docs/manual/mod/mod_alias.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ Alias "/image" "/ftp/pub/image"
</LocationMatch>
</highlight>

<p>Note that when the <directive>AliasPreservePath</directive>
directive is on, the full path is mapped to the destination. When
the directive is off, all URLs are mapped to the single target
URL.</p>

<highlight language="config">
# /files/foo and /files/bar mapped to /ftp/pub/files/foo and /ftp/pub/files/bar
&lt;Location "/files"&gt;
AliasPreservePath on
Alias "/ftp/pub/files"
&lt;/Location&gt;
# /errors/foo and /errors/bar mapped to /var/www/errors.html
&lt;Location "/errors"&gt;
AliasPreservePath off
Alias "/var/www/errors.html"
&lt;/Location&gt;
</highlight>

</usage>
</directivesynopsis>

Expand Down Expand Up @@ -641,5 +659,30 @@ ScriptAliasMatch "(?i)^/cgi-bin(.*)" "/usr/local/apache/cgi-bin$1"
</usage>
</directivesynopsis>

<directivesynopsis>
<name>AliasPreservePath</name>
<description>Map the full path after the alias in a location.</description>
<syntax>AliasPreservePath OFF|ON</syntax>
<default>AliasPreservePath OFF</default>
<contextlist><context>server config</context><context>virtual host</context>
<context>directory</context>
</contextlist>
<compatibility>2.5.1 and later</compatibility>

<usage>
<p>When using the two parameter version of the
<directive>Alias</directive> directive, the full path after the alias
is preserved. When using the one parameter version of the
<directive>Alias</directive> directive inside a
<directive>Location</directive> directive, the full path is dropped,
and all URLs are mapped to the target expression.</p>

<p>To make the one parameter version of the
<directive>Alias</directive> directive preserve paths in the same way
that the two parameter version of the <directive>Alias</directive>
directive, enable this setting.</p>

</usage>
</directivesynopsis>

</modulesynopsis>
13 changes: 12 additions & 1 deletion modules/mappers/mod_alias.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#define ALIAS_FLAG_OFF 0
#define ALIAS_FLAG_ON 1

#define ALIAS_PRESERVE_PATH_DEFAULT 0

typedef struct {
const char *real;
const char *fake;
Expand All @@ -64,6 +66,7 @@ typedef struct {
const ap_expr_info_t *redirect;
int redirect_status; /* 301, 302, 303, 410, etc */
int allow_relative; /* skip ap_construct_url() */
int alias_preserve_path; /* map full path */
} alias_dir_conf;

module AP_MODULE_DECLARE_DATA alias_module;
Expand All @@ -89,6 +92,7 @@ static void *create_alias_dir_config(apr_pool_t *p, char *d)
(alias_dir_conf *) apr_pcalloc(p, sizeof(alias_dir_conf));
a->redirects = apr_array_make(p, 2, sizeof(alias_entry));
a->allow_relative = ALIAS_FLAG_DEFAULT;
a->alias_preserve_path = ALIAS_FLAG_DEFAULT;
return a;
}

Expand Down Expand Up @@ -124,6 +128,10 @@ static void *merge_alias_dir_config(apr_pool_t *p, void *basev, void *overridesv
a->allow_relative = (overrides->allow_relative != ALIAS_FLAG_DEFAULT)
? overrides->allow_relative
: base->allow_relative;
a->alias_preserve_path = (overrides->alias_preserve_path != ALIAS_FLAG_DEFAULT)
? overrides->alias_preserve_path
: base->alias_preserve_path;

return a;
}

Expand Down Expand Up @@ -443,7 +451,7 @@ static char *try_alias(request_rec *r)
return PREGSUB_ERROR;
}

if (dirconf->alias_fake) {
if (dirconf->alias_fake && dirconf->alias_preserve_path == ALIAS_FLAG_ON) {
int l;

l = alias_matches(r->uri, dirconf->alias_fake);
Expand Down Expand Up @@ -764,6 +772,9 @@ static const command_rec alias_cmds[] =
AP_INIT_FLAG("RedirectRelative", ap_set_flag_slot,
(void*)APR_OFFSETOF(alias_dir_conf, allow_relative), OR_FILEINFO,
"Set to ON to allow relative redirect targets to be issued as-is"),
AP_INIT_FLAG("AliasPreservePath", ap_set_flag_slot,
(void*)APR_OFFSETOF(alias_dir_conf, alias_preserve_path), OR_FILEINFO,
"Set to ON to map the full path after the fakename to the realname."),

{NULL}
};
Expand Down

0 comments on commit bc8431b

Please sign in to comment.