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

IIS URL重写规则问题:添加末尾斜杠时排除文件及特定路径

Fix IIS URL Rewrite: Add Trailing Slash Without Breaking Files & Exclude Paths

Got it, let's tackle this problem head-on. The issue with your current rule is that it doesn't distinguish between actual files (like your .svg images) and directories/clean URLs—so it's incorrectly appending a slash to files, which leads to 404s because the server can't find a "file with a slash at the end." The CSS working is probably a fluke (maybe browser caching, existing rules handling CSS requests, or server config quirks), but we'll fix this universally.

Here's the corrected rule that excludes all existing files and lets you add specific path exclusions:

<rewrite>
  <rules>
    <rule name="Add Trailing Slash" stopProcessing="true">
      <match url="(.*[^/])$" />
      <conditions logicalGrouping="MatchAll">
        <!-- Exclude any actual existing files (prevents .svg, .png, etc. from being redirected) -->
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <!-- Add as many of these as needed to exclude specific paths -->
        <add input="{REQUEST_URI}" pattern="^/your-specific-path/" negate="true" />
        <!-- Optional: If you only want to target directories, uncomment this line -->
        <!-- <add input="{REQUEST_FILENAME}" matchType="IsDirectory" /> -->
      </conditions>
      <action type="Redirect" url="{R:1}/" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

Let's break down what each part does:

  • Match Pattern: (.*[^/])$ targets any URL that doesn't end with a slash—this is what your original rule probably used, but we're adding guardrails.
  • Exclude Existing Files: The condition <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> tells IIS: "Only apply this rule if the requested path isn't an actual file on the server." This stops it from touching .svg, .css, .js, or any other file type.
  • Exclude Specific Paths: The second condition lets you skip certain URLs entirely. For example, if you don't want to add slashes to API endpoints like /api/v1/users, replace /your-specific-path/ with /api/ (you can add multiple <add> lines here for different paths).
  • Optional Directory-Only Target: If you only want to add slashes to actual directories (not clean URLs for dynamic content), uncomment the IsDirectory condition—this will only redirect paths that map to a physical folder on the server.

Why your original rule broke images:

Without the IsFile negation, IIS sees /globalassets/icons/image.svg (no trailing slash) and matches it, redirecting to /globalassets/icons/image.svg/. Since there's no file named image.svg/ on your server, you get a 404. The CSS working was likely a coincidence—maybe the CSS request was cached before the rule was applied, or another rule was handling it first.

To implement this:

  1. Open IIS Manager, navigate to your site, and open the URL Rewrite module.
  2. Either edit your existing "Add Trailing Slash" rule, or create a new one.
  3. Copy the conditions and adjust the pattern/exclusions to match your needs.
  4. Test it out: Visit an image URL (like your .svg)—it shouldn't redirect. Visit a directory or clean URL without a slash, and it should redirect to the version with a trailing slash.

内容的提问来源于stack exchange,提问作者Ayo Adesina

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:04:55