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.
On this page · 5 min · Advanced
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Oct 30, 2026
Related tutorials
Interview questions
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#
<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>
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#
- {C:1} refers to a capture from the condition; {R:1} from the match.
- The www rule redirects to the apex domain.
- {ToLower:} creates lowercase URLs.
- 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#
Rewrite doesn’t change the browser URL, so duplicate URLs remain. Use 301 for moved pages.
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
<rule name="Blog slugs" stopProcessing="true">
<match url="^blog/([a-z0-9-]+)$" />
<action type="Rewrite" url="blog.aspx?slug={R:1}" />
</rule>
Mini quiz#
Which action changes the URL shown in the browser?
Want more? Practise all IIS MCQs · IIS interview questions
My notes#
Saved in this browser.