URL Rewriter Managed Fusion strip a QueryString
I'm Using Managed Fusion URL Rewriter, so far it has been awesome. Now I just need to make sure a query string is passed in a URL from a proxy web开发者_运维知识库site. I Need to pass CODE=777 on every call so I have the code below:
RewriteRule ^(.*) http://www.somewebsite.com/$1?CODE=777[P, QSA,I,L]
However I need to strip the query string before hand. So I used the code below:
RewriteCond %{QUERY_STRING} ^CODE=([0-9]+)+$
RewriteRule ^(.*) http://www.somewebsite.com/$1? [P, QSA]
RewriteRule ^(.*) http://www.somewebsite.com/$1?CODE=777[P, QSA,I,L]
But it fails when I redirect with additional querystring such as below
http://proxyserver.com?othercode=something
Any Ideas how to strip a particular querystring without removing all query strings paramters?
I downloaded the source code from Managed Fusion and added the project to my solution.
I searched and found this bit of code below that appends the QueryString. I updated the code by adding a new method to remove the querystring CODE and affix a FIXED one.
I also added a Setting to control the FIXED Value assigned to the CODE.
private Uri AppendQueryString(Uri substituedUrl, Uri existingUrl)
{
string append = existingUrl.Query.TrimStart('?');
if (!String.IsNullOrEmpty(append))
{
UriBuilder builder = new UriBuilder(substituedUrl);
if (String.IsNullOrEmpty(builder.Query) || builder.Query == "?")
builder.Query = append;
else
builder.Query = builder.Query.TrimStart('?') + "&" + append;
return AppendFixedQueryString(builder.Uri, existingUrl);
}
return AppendFixedQueryString(substituedUrl, existingUrl);
}
ADDED NEW METHOD
private Uri AppendFixedQueryString(Uri substituedUrl, Uri existingUrl)
{
string append = string.Format("CODE={0}", Settings.Default.CODE);
if (!String.IsNullOrEmpty(append))
{
UriBuilder builder = new UriBuilder(substituedUrl);
builder.Query = Regex.Replace(builder.Query, @"CODE=([0-9]+)", String.Empty);
if (String.IsNullOrEmpty(builder.Query) || builder.Query == "?")
builder.Query = append;
else
builder.Query = builder.Query.TrimStart('?').Trim("&".ToCharArray()) + "&" + append;
return builder.Uri;
}
return substituedUrl;
}
With this modification. even if the user explictly types CODE=123 at the URL it will just be ignored and a fixed CODE value will be assigned
精彩评论