Guo Hong Lim
Guo Hong Lim

Reputation: 1710

ASP.NET rewrite rules in web.config

I was attempting to redirect

/KEY/TEXT or

/KEY/WEB.ASPX

to redirector?key=KEY&url=TEXT or redirector?key=KEY&url=WEB.ASPX respectively.

I managed to gain success for /KEY in which I wrote the following in my web.config

<match url="^([a-zA-Z0-9_-]+)$" ignoreCase="false" />
<action type="Rewrite" url="/redirector.aspx?key={R:1}" appendQueryString="false" />

however whichever way I attemped to match the URL for /TEXT or /WEB.ASPX I was not able to get. I even attempted

<match url="^([a-zA-Z0-9_-]+)(\\/)([a-zA-Z0-9_-]+)$" ignoreCase="false" />
<action type="Rewrite" url="/redirector.aspx?key={R:1}&url={R:2}" appendQueryString="false" />

Anyone could highlight what went wrong in the following request?

Thanks!

Upvotes: 1

Views: 750

Answers (1)

Adrian Iftode
Adrian Iftode

Reputation: 15663

<match url="^([a-zA-Z0-9_\-]+)/+([a-zA-Z0-9_\-\.]+)$" ignoreCase="false" />
<action type="Rewrite" url="/redirector.aspx?key={R:1}&amp;url={R:2}" />

See the differences

  • – and . characters have to be escaped, because they are special characters
  • You don't have to escape the following character /
  • Each capture is required, so you can match /KEY/TEXT and ignore urls like /KEY/
  • instead of &, use the xml entity &amp;

This is a rewrite rule, that means you would still see the url in browsers nav bar. If you want to redirect to redirector.aspx, then you have to use type="Redirect"

Upvotes: 2

Related Questions