Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS URL Rewrite Module : Redirect Based On QueryString

I Have some problems with redirecting to another URL based on the query string parameters. I want to redirect users which enter www.domain.com/signup.aspx?p=1 to:

www.domain.com/signup

<rule name="Signup Redirect 1" stopProcessing="true">   <match url="signup\.aspx\?p=1" />   <conditions logicalGrouping="MatchAll" />   <action type="Redirect" url="signup" redirectType="Temporary" /> </rule> 

Now when they enter www.domain.com/signup.aspx?p=2 they must go to:

www.domain.com/signup/promocode

<rule name="Signup Redirect 2" stopProcessing="true">   <match url="signup\.aspx\?p=2" />   <conditions logicalGrouping="MatchAll" />   <action type="Redirect" url="signup/promocode" redirectType="Temporary" /> </rule> 

The above rules don't work. What is the right way to do this? Thanks in Advance.

Gr

Martijn

like image 581
Martijn B Avatar asked Feb 13 '10 12:02

Martijn B


People also ask

How do I redirect a specific URL in IIS?

In IIS Manager, expand the local computer, right-click the Web site or directory you want to redirect, and click Properties. Click the Home Directory, Virtual Directory, or Directory tab. Under The content for this source should come from, click A redirection to a URL.

How do I create a redirect rule in IIS?

Creating a redirect rule A redirect rule enables more than one URL to point to a single Web page. To do this, open the URL Rewrite feature view UI in IIS Manager. Click Add Rule(s)…, and then select the Blank Rule template again.

How do I fix the URL Rewrite module in IIS?

IIS Rewrite Module ProblemUninstall the Rewrite Module from Windows Features. Go to the Web Platform Installer. Pick Url Rewrite from Products | Server section and install. Restart IIS.

What is the difference between URL Rewrite and redirect?

Simply put, a redirect is a client-side request to have the web browser go to another URL. This means that the URL that you see in the browser will update to the new URL. A rewrite is a server-side rewrite of the URL before it's fully processed by IIS.


2 Answers

A more robust method of using a value to select a destination is to use Rewrite Maps. The map is essentially a lookup table. This doesn't require a new rule (and an additional evaluation of the URL against a pattern on every request) for every new path.

<rules>   <rule name="Signup Redirect Map" stopProcessing="true">     <match url="^signup\.aspx$" />     <conditions logicalGrouping="MatchAll" trackAllCaptures="true">       <add input="{QUERY_STRING}" pattern="p=([^&amp;]+)" />       <add input="{Signups:{C:1}}" pattern="(.+)" />     </conditions>     <action type="Redirect" url="{C:2}" redirectType="Temporary" />   </rule> </rules> <rewriteMaps>   <rewriteMap name="Signups">     <add key="1" value="signup" />     <add key="2" value="signup/promocode" />     <add key="3" value="signup/newcode" />     <add key="n" value="signup/futureproof" />   </rewriteMap> </rewriteMaps> 

Definitions:

  • {C:1} is a backreference to the first condition match: the query string value.
  • {Signups:{C:1}} is an instruction to look up {C:1} in the Signups map.
  • {C:2} is a backreference to the second condition match: the value from the Signups map.
like image 125
jpj625 Avatar answered Oct 04 '22 10:10

jpj625


See if this works a bit better:

<rule name="Signup Redirect 1" stopProcessing="true">   <match url="signup\.aspx$" />   <conditions>     <add input="{QUERY_STRING}" pattern="p=1" />   </conditions>   <action type="Redirect" url="signup" redirectType="Temporary" /> </rule>  <rule name="Signup Redirect 2" stopProcessing="true">   <match url="signup\.aspx$" />   <conditions>     <add input="{QUERY_STRING}" pattern="p=2" />   </conditions>   <action type="Redirect" url="signup/promocode" redirectType="Temporary" /> </rule> 
like image 25
Nick Craver Avatar answered Oct 04 '22 10:10

Nick Craver