如何配置IIS Rewrite模块将.jpg重写为.ashx并保留查询参数?
image.jpg with image.ashx Hey there, let's get this rewrite rule sorted for you. Since you already have a wildcard rule in place, we can either tweak that or add a targeted rule to handle the .jpg to .ashx swap while preserving all your query string parameters intact.
Targeted Rewrite Rule for Root image.jpg
Add this rule to your web.config under the <rules> section of the <rewrite> node:
<rule name="Swap image.jpg to image.ashx" stopProcessing="false"> <match url="^image\.jpg$" /> <action type="Rewrite" url="image.ashx" appendQueryString="true" /> </rule>
Let's break down what each part does:
name: A descriptive label to help you identify this rule later in IIS Manager.match url: The regex^image\.jpg$ensures we only match requests exactly forimage.jpgat your domain root. If your image lives in a subfolder (like/assets/image.jpg), adjust this to^assets/image\.jpg$.action type="Rewrite": Tells IIS to internally rewrite the request (users won't see the.ashxin their browser address bar).appendQueryString="true": This critical setting automatically preserves all your query parameters (vehicle=...,capid=..., etc.) without you having to manually define them in the rule.
Flexible Rule for image.jpg Anywhere in the Path
If you need to match image.jpg in subfolders too, use this expanded version:
<rule name="Swap image.jpg to image.ashx (any path)" stopProcessing="false"> <match url="(.*)image\.jpg$" /> <action type="Rewrite" url="{R:1}image.ashx" appendQueryString="true" /> </rule>
Here, (.*) captures everything before image.jpg into the backreference {R:1}, so a request like /media/image.jpg?params will rewrite to /media/image.ashx?params.
Pro Tip: Test Before Deploying
Use the Test Pattern tool in IIS Manager's URL Rewrite module to paste your original URL and verify the match/rewrite works as expected—this avoids any surprises once the rule is live.
内容的提问来源于stack exchange,提问作者Ben Durkin

