简体   繁体   中英

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!

<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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM