You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置IIS Rewrite模块将.jpg重写为.ashx并保留查询参数?

IIS URL Rewrite Rule to Swap 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 for image.jpg at 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 .ashx in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:18:32