Search code examples
iisurl-rewritingweb-config

IIS web.config url rewriting


I'm using the following rule in my web.config:

<?xml version="1.0" encoding="UTF-8"?>
 <configuration>
  <system.webServer>
   <rewrite>
    <rules>
     <rule name="rewrite">
      <match url="([0-9]+)/([0-9]+)" />
      <action type="Rewrite" url="index.php?year={R:1}&amp;item={R:2}" />
     </rule>
    </rules>
   </rewrite>
 </system.webServer>
</configuration>

It works.

But I need to show a 404 error if user writes one or more letters in the second query (item), e.g. 1b, or 1bla, etc.

It should only be allowed a (positive) number and, possibly, a query like ?utm_source=anothersite or ?ref=anothersite etc.

In other words, url should be:

www.mysite.com/{year}/{item}

where {year} and {item} are both mandatory and could be numbers only, no other elements allowed (e.g.: www.mysite.com/{year}/{item}/{somethingelse}), except ?key=value.

How can I change match url value in order to achieve that result?

Thanks.


Solution

  • Please add a "$" to the end your pattern.

    <match url="([0-9]+)/([0-9]+)$" />
    

    This is my tested result:

    enter image description here

    enter image description here