From: Rich Bowen Date: Tue, 29 Dec 2009 13:34:24 +0000 (+0000) Subject: Adds a [QSD] flag to RewriteRule to discard unwanted query strings X-Git-Tag: 2.3.5~46 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c4b0fa2f7d0e268a094884c4f2349fe837f06ad0;p=apache Adds a [QSD] flag to RewriteRule to discard unwanted query strings without the old kludge of sticking a ? on the end of the target URI. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@894383 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/docs/manual/mod/mod_rewrite.html.en b/docs/manual/mod/mod_rewrite.html.en index 1bf19c5cff..ab1f2600ca 100644 --- a/docs/manual/mod/mod_rewrite.html.en +++ b/docs/manual/mod/mod_rewrite.html.en @@ -1370,6 +1370,14 @@ cannot use $N in the substitution string! data to the query string via a rewrite rule. This rule has no net effect unless your substitution explicitly provides a new query string. +
'qsdiscard|QSD' + (query string discard)
+ Discards any query string attached to the incoming URI. Without + this flag, the query string will be automatically copied from + the request URI to the rewritten URL. Note, however, that if you + provide a query string on your target URI, that one will be used + instead.
+
'redirect|R [=code]' (force redirect)

Prefix Substitution with diff --git a/docs/manual/mod/mod_rewrite.xml b/docs/manual/mod/mod_rewrite.xml index 4e75300c1c..ce74347cb4 100644 --- a/docs/manual/mod/mod_rewrite.xml +++ b/docs/manual/mod/mod_rewrite.xml @@ -1387,6 +1387,14 @@ cannot use $N in the substitution string! data to the query string via a rewrite rule. This rule has no net effect unless your substitution explicitly provides a new query string.

+
'qsdiscard|QSD' + (query string discard)
+ Discards any query string attached to the incoming URI. Without + this flag, the query string will be automatically copied from + the request URI to the rewritten URL. Note, however, that if you + provide a query string on your target URI, that one will be used + instead.
+
'redirect|R [=code]' (force redirect)
diff --git a/docs/manual/rewrite/flags.html.en b/docs/manual/rewrite/flags.html.en index 31c2089c8a..811393f364 100644 --- a/docs/manual/rewrite/flags.html.en +++ b/docs/manual/rewrite/flags.html.en @@ -363,6 +363,27 @@ will be discarded.

+

QSD|qsdiscard

+

+When the requested URI contains a query string, and the target URI does +not, the default behavior of RewriteRule is to copy that query +string to the target URI. Using the [QSD] flag causes the query string +to be discarded. +

+ +

+Using [QSD] and [QSA] together will result in [QSD] taking preference. +

+ +

+If the target URI has a query string, the default behavior will be +observed - that is, the original query string will be discarded and +replaced with the query string in the RewriteRule target +URI. +

+ + +

R|redirect

Use of the [R] flag causes a HTTP redirect to be issued to the browser. diff --git a/docs/manual/rewrite/flags.xml b/docs/manual/rewrite/flags.xml index cc20f8c21d..23a12e2180 100644 --- a/docs/manual/rewrite/flags.xml +++ b/docs/manual/rewrite/flags.xml @@ -377,6 +377,28 @@ will be discarded.

+
QSD|qsdiscard +

+When the requested URI contains a query string, and the target URI does +not, the default behavior of RewriteRule is to copy that query +string to the target URI. Using the [QSD] flag causes the query string +to be discarded. +

+ +

+Using [QSD] and [QSA] together will result in [QSD] taking preference. +

+ +

+If the target URI has a query string, the default behavior will be +observed - that is, the original query string will be discarded and +replaced with the query string in the RewriteRule target +URI. +

+ +
+
R|redirect

Use of the [R] flag causes a HTTP redirect to be issued to the browser. diff --git a/modules/mappers/mod_rewrite.c b/modules/mappers/mod_rewrite.c index 43e586625f..dd01c813ab 100644 --- a/modules/mappers/mod_rewrite.c +++ b/modules/mappers/mod_rewrite.c @@ -153,6 +153,7 @@ static void (*dbd_prepare)(server_rec*, const char*, const char*) = NULL; #define RULEFLAG_STATUS 1<<13 #define RULEFLAG_ESCAPEBACKREF 1<<14 #define RULEFLAG_DISCARDPATHINFO 1<<15 +#define RULEFLAG_QSDISCARD 1<<16 /* return code of the rewrite rule * the result may be escaped - or not @@ -754,7 +755,7 @@ static char *escape_absolute_uri(apr_pool_t *p, char *uri, unsigned scheme) * split out a QUERY_STRING part from * the current URI string */ -static void splitout_queryargs(request_rec *r, int qsappend) +static void splitout_queryargs(request_rec *r, int qsappend, int qsdiscard) { char *q; @@ -770,6 +771,11 @@ static void splitout_queryargs(request_rec *r, int qsappend) return; } + if ( qsdiscard ) { + r->args = NULL; /* Discard query string */ + rewritelog((r, 2, NULL, "discarding query string")); + } + q = ap_strchr(r->filename, '?'); if (q != NULL) { char *olduri; @@ -3493,6 +3499,9 @@ static const char *cmd_rewriterule_setflag(apr_pool_t *p, void *_cfg, if ( !strcasecmp(key, "SA") || !strcasecmp(key, "sappend")) { /* qsappend */ cfg->flags |= RULEFLAG_QSAPPEND; + } else if ( !strcasecmp(key, "SD") + || !strcasecmp(key, "sdiscard") ) { /* qsdiscard */ + cfg->flags |= RULEFLAG_QSDISCARD; } else { ++error; @@ -3998,7 +4007,7 @@ static int apply_rewrite_rule(rewriterule_entry *p, rewrite_ctx *ctx) r->path_info = NULL; } - splitout_queryargs(r, p->flags & RULEFLAG_QSAPPEND); + splitout_queryargs(r, p->flags & RULEFLAG_QSAPPEND, p->flags & RULEFLAG_QSDISCARD); /* Add the previously stripped per-directory location prefix, unless * (1) it's an absolute URL path and