Hi! I have a web form application that shows a user a clickable link in a table. The link pops up a new window for the user. However, I want to change the link displayed in the window to a friendly URL, but ALSO want to hide the query string parameters.
For example, a user would click a link in the table that pops up a window to http://www.example.com/test.aspx?a=123&b=456. I want the new window to show only http://www.example.com/test.
I'm able to use the User-friendly URL template rule to go from http://www.example.com/test.aspx?a=123&b=456 to http://www.example.com/test/123/456.
What I want to do is put the query parameters a (e.g.) to a custom server variable HTTP_QUERY_A and the query parameter b (e.g. 456) to a custom server variable HTTP_QUERY_B in the redirect rule. Then in the rewrite rule, I want to match http://www.example.com/test
and build a rewrite URL using the custom server variables HTTP_QUERY_A and HTTP_QUERY_B. However, these custom server variables are null/blank. I know that they exist, because I can access them with an Outbound rewrite rule. Because they are blank, the rewrite
URL doesn't work.
I've made sure that both the server variables are in the allowedServerVariables list. I've read everything I can find on server variables and user-friendly URLs but can't figure this out. I've also tried changing the stop processing to false, but that didn't
work.
Thanks for catching my missing .aspx in the rule. However, the server variables HTTP_QUERY_A and HTTP_QUERY_B are still null/blank in the rewrite rule. The URL is rewritten as
http://www.example.com/test.aspx?a=&1b=
Can URL Rewrite share custom server variables? I've also tried HTTP_X_QUERY_A and RESPONSE_QUERY_A for the server variable names too.
It's unfortunate that current version of URL Rewrite Module does not support dynamically set server variable value. You can use rewrite map to set server variable value statically . or you may need to write a custom provider to set server variable value.
kcarlson4
2 Posts
Passing a custom Server Variable from a Redirect rule to a Rewrite rule
Apr 06, 2012 08:29 PM|LINK
Hi! I have a web form application that shows a user a clickable link in a table. The link pops up a new window for the user. However, I want to change the link displayed in the window to a friendly URL, but ALSO want to hide the query string parameters.
For example, a user would click a link in the table that pops up a window to http://www.example.com/test.aspx?a=123&b=456. I want the new window to show only http://www.example.com/test.
I'm able to use the User-friendly URL template rule to go from http://www.example.com/test.aspx?a=123&b=456 to http://www.example.com/test/123/456.
What I want to do is put the query parameters a (e.g.) to a custom server variable HTTP_QUERY_A and the query parameter b (e.g. 456) to a custom server variable HTTP_QUERY_B in the redirect rule. Then in the rewrite rule, I want to match http://www.example.com/test and build a rewrite URL using the custom server variables HTTP_QUERY_A and HTTP_QUERY_B. However, these custom server variables are null/blank. I know that they exist, because I can access them with an Outbound rewrite rule. Because they are blank, the rewrite URL doesn't work.
I've made sure that both the server variables are in the allowedServerVariables list. I've read everything I can find on server variables and user-friendly URLs but can't figure this out. I've also tried changing the stop processing to false, but that didn't work.
Here are the rules:
<rule name="RedirectUserFriendlyURL" stopProcessing="true">
<match url="^test$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^a=([^=&]+)&b=([^=&]+)$" />
</conditions>
<action type="Redirect" url="test" appendQueryString="false" />
<serverVariables>
<set name="HTTP_QUERY_A" value="{C:1}" />
<set name="HTTP_QUERY_B" value="{C:2}" />
</serverVariables>
</rule>
<rule name="RewriteUserFriendlyURL" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="test?a={HTTP_QUERY_A}&b={HTTP_QUERY_B}" />
</rule>
Can anybody help me figure out how to do this? Thanks in advance!
Regards,
Kurt
URL Rewrite server variables
Dalong Zhang...
647 Posts
Microsoft
Re: Passing a custom Server Variable from a Redirect rule to a Rewrite rule
Apr 09, 2012 09:05 AM|LINK
Hi,
I adjusted your rule for your usage:
<rule name="RedirectUserFriendlyURL" stopProcessing="true">
<match url="^test\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^a=([^=&]+)&b=([^=&]+)$" />
</conditions>
<action type="Redirect" url="test" appendQueryString="false" />
<serverVariables>
<set name="HTTP_QUERY_A" value="{C:1}" />
<set name="HTTP_QUERY_B" value="{C:2}" />
</serverVariables>
</rule>
<rule name="RewriteUserFriendlyURL" stopProcessing="true">
<match url="^test$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="test.aspx?a={HTTP_QUERY_A}&b={HTTP_QUERY_B}" />
</rule>
With above rule, You should be able to use your custom server variable.
Feedback to us
Develop and promote your apps in Windows Store
kcarlson4
2 Posts
Re: Passing a custom Server Variable from a Redirect rule to a Rewrite rule
Apr 09, 2012 02:09 PM|LINK
Thanks for catching my missing .aspx in the rule. However, the server variables HTTP_QUERY_A and HTTP_QUERY_B are still null/blank in the rewrite rule. The URL is rewritten as http://www.example.com/test.aspx?a=&1b=
Can URL Rewrite share custom server variables? I've also tried HTTP_X_QUERY_A and RESPONSE_QUERY_A for the server variable names too.
Regards,
Kurt
Dalong Zhang...
647 Posts
Microsoft
Re: Passing a custom Server Variable from a Redirect rule to a Rewrite rule
Apr 10, 2012 09:11 AM|LINK
Hi,
It's unfortunate that current version of URL Rewrite Module does not support dynamically set server variable value. You can use rewrite map to set server variable value statically . or you may need to write a custom provider to set server variable value.
Setting HTTP request headers and IIS server variables
http://learn.iis.net/page.aspx/686/setting-http-request-headers-and-iis-server-variables/
Developing a Custom Rewrite Provider for URL Rewrite Module
http://learn.iis.net/page.aspx/804/developing-a-custom-rewrite-provider-for-url-rewrite-module/
Feedback to us
Develop and promote your apps in Windows Store