IIS (Internet Information Services) · Performance & Behaviour

URL Rewrite

URL Rewrite lets IIS change or redirect URLs before your app sees them — for SEO-friendly URLs, HTTPS redirects and single-page apps.

Advanced 5 min Lesson 24 of 33Covers IIS 10 on Windows Server 2019/2022/2025 and Windows 10/11.
0/33 in IIS
On this page · 5 min · Advanced

In simple words#

URL Rewrite is the post office sorting desk. A rewrite quietly delivers a letter to a different room without telling the sender. A redirect sends the letter back stamped “moved — please use this new address”.

Rewrite
Server-side change; the browser URL stays the same.
Redirect
Browser is told to go to a new URL (301/302).
{R:1}
Back-reference to a captured group in the match.

Learning objectives#

  • Install the URL Rewrite module
  • Write rules with patterns and conditions
  • Choose rewrite or redirect
  • Use common SEO rules

Explanation#

URL Rewrite is a free module (download separately). Rules live in <system.webServer><rewrite><rules>.

A rule has a match (regex on the URL path, without the leading slash), optional conditions (server variables like {HTTP_HOST}, {HTTPS}, {REQUEST_FILENAME}) and an action.

Actions: Rewrite (internal), Redirect (301 permanent / 302 temporary), CustomResponse, AbortRequest.

Rules run in order; stopProcessing="true" ends processing when a rule matches.

Code example: Common SEO rules#

XMLweb.config Illustrative
<rewrite>
  <rules>
    <rule name="www to apex" stopProcessing="true">
      <match url="(.*)" />
      <conditions><add input="{HTTP_HOST}" pattern="^www\.(.+)$" /></conditions>
      <action type="Redirect" url="https://{C:1}/{R:1}" redirectType="Permanent" />
    </rule>


    <rule name="Lowercase URLs" stopProcessing="true">
      <match url=".*[A-Z].*" ignoreCase="false" />
      <action type="Redirect" url="{ToLower:{URL}}" redirectType="Permanent" />
    </rule>


    <rule name="Old product URLs" stopProcessing="true">
      <match url="^product\.aspx$" />
      <conditions><add input="{QUERY_STRING}" pattern="id=(\d+)" /></conditions>
      <action type="Redirect" url="/products/{C:1}" appendQueryString="false" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>
Expected output
www.shop.example.com/About      → 301 → shop.example.com/About → 301 → /about
/product.aspx?id=42             → 301 → /products/42

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. {C:1} refers to a capture from the condition; {R:1} from the match.
  2. The www rule redirects to the apex domain.
  3. {ToLower:} creates lowercase URLs.
  4. Old .aspx links redirect permanently to new clean URLs.

Real-world use case#

A site migrating from Web Forms to ASP.NET Core keeps its Google rankings by 301-redirecting every old .aspx URL.

Common mistakes#

Using Rewrite when you meant Redirect

Rewrite doesn’t change the browser URL, so duplicate URLs remain. Use 301 for moved pages.

Redirect loops

Add conditions so a rule doesn’t match its own target. Test with curl -I.

Best practices#

  • Test rules with the Test Pattern tool
  • 301 for permanent moves
  • stopProcessing on terminal rules
  • Keep rules in source control

Remember this#

Rewrite = internal. Redirect = browser moves.

match on path, conditions on server variables.

{R:n} match capture, {C:n} condition capture.

Practice exercise#

Your task: Rewrite /blog/my-post to /blog.aspx?slug=my-post internally.

Show solution
XML Illustrative
<rule name="Blog slugs" stopProcessing="true">
  <match url="^blog/([a-z0-9-]+)$" />
  <action type="Rewrite" url="blog.aspx?slug={R:1}" />
</rule>

Mini quiz#

URL Rewrite quizMultiple choice Question 1 of 1

Which action changes the URL shown in the browser?

Score so far: 0/0

Want more? Practise all IIS MCQs · IIS interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.