<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <!--
          Server-side locale redirect: reads the 'locale' cookie and issues a 307
          before sending any HTML, eliminating the client-side double-load.
          Must be first so it fires before trailing-slash and other rewrite rules.
          Only applies to page requests (not static assets) on unprefixed paths.
        -->
        <!-- 301 redirects: old docs blog URLs → landing site blog -->
        <rule name="BlogRedirectLocalized" stopProcessing="true">
          <match url="^docs/(nl|es|pt|de|fr|ja|ru|ar|zh)/blog/(.*)$" />
          <action type="Redirect" url="/{R:1}/blog/{R:2}" redirectType="Permanent" />
        </rule>
        <rule name="BlogRedirectDefault" stopProcessing="true">
          <match url="^docs/blog/(.*)$" />
          <action type="Redirect" url="/blog/{R:1}" redirectType="Permanent" />
        </rule>

        <rule name="LocaleRedirect" stopProcessing="true">
          <match url="^((?!(?:nl|es|pt|de|fr|ja|ru|ar|zh|docs)(?:/|$)).*)$" />
          <conditions logicalGrouping="MatchAll">
            <add input="{URL}" pattern="\.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|webp|map|json|txt|xml|pdf|zip|ex4|ex5)$" negate="true" />
            <add input="{HTTP_COOKIE}" pattern="(?:^|;\s*)locale=(nl|es|pt|de|fr|ja|ru|ar|zh)(?:;|$)" />
          </conditions>
          <action type="Redirect" url="/{C:1}/{R:1}" redirectType="Temporary" />
        </rule>

        <!-- Locale redirect for /docs/* paths: /docs/get-started/ → /docs/nl/get-started/ -->
        <rule name="LocaleRedirectDocs" stopProcessing="true">
          <match url="^docs/((?!(?:nl|es|pt|de|fr|ja|ru|ar|zh)(?:/|$)).*)$" />
          <conditions logicalGrouping="MatchAll">
            <add input="{URL}" pattern="\.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|webp|map|json|txt|xml|pdf|zip|ex4|ex5|pagefind|pf_meta|pf_index|pf_fragment)$" negate="true" />
            <add input="{HTTP_COOKIE}" pattern="(?:^|;\s*)locale=(nl|es|pt|de|fr|ja|ru|ar|zh)(?:;|$)" />
          </conditions>
          <action type="Redirect" url="/docs/{C:1}/{R:1}" redirectType="Temporary" />
        </rule>

        <!-- Redirect to trailing slash if missing and is a directory -->
        <rule name="RedirectToTrailingSlash" stopProcessing="true">
          <match url="^(.*[^/])$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
          </conditions>
          <action type="Redirect" url="/{R:1}/" redirectType="Permanent" />
        </rule>

        <rule name="HTML to Folder Index" stopProcessing="true">
          <match url="^([^/]+)\.html$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{DOCUMENT_ROOT}/{R:1}/index.html" matchType="IsFile" />
          </conditions>
          <action type="Rewrite" url="{R:1}/index.html" />
        </rule>
        
        <rule name="IndexHTML to Folder" stopProcessing="true">
          <match url="^(.*)/index\.html$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" />
          </conditions>
          <action type="Rewrite" url="{R:1}" />
        </rule>

        <!-- Remove trailing slash without redirect -->
        <rule name="RemoveTrailingSlash" stopProcessing="true">
          <match url="(.*)/$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          </conditions>
          <action type="Rewrite" url="{R:1}" />
        </rule>

        <!-- Redirects for legacy .html pages -->       
        <rule name="Redirect sitemap.xml to sitemap-index" stopProcessing="true">
          <match url="sitemap.xml" />
          <action type="Redirect" url="/sitemap-index.xml" redirectType="Permanent" />
        </rule>        

        <rule name="Redirect index.html to root" stopProcessing="true">
          <match url="index.html" />
          <action type="Redirect" url="/" redirectType="Permanent" />
        </rule>               
        
        <!-- Fixed Redirect from www to non-www -->
        <rule name="RedirectWwwToNonWww" 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="Redirect maintenance to homepage" patternSyntax="Wildcard" stopProcessing="true">
          <match url="*maintenance.html" />
          <action type="Redirect" url="/" redirectType="Permanent" />
        </rule>        

        <!-- Redirect any .html file not otherwise matched to its clean path without .html -->
        <rule name="Redirect leftover .html to clean URL" stopProcessing="true">
          <match url="^(.*)\.html$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" />
          </conditions>
          <action type="Redirect" url="/{R:1}" redirectType="Permanent" />
        </rule>
      </rules>
      <outboundRules>
        <rule name="NoCacheHtml">
          <match serverVariable="RESPONSE_Cache_Control" pattern=".*" />
          <conditions>
            <add input="{RESPONSE_CONTENT_TYPE}" pattern="text/html" />
          </conditions>
          <action type="Rewrite" value="no-store" />
        </rule>
      </outboundRules>
    </rewrite>

    <staticContent>
      <mimeMap fileExtension="." mimeType="text/plain" />
      <mimeMap fileExtension=".webp" mimeType="image/webp" />
      <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
      <mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
      <mimeMap fileExtension=".json" mimeType="application/json" />
      <mimeMap fileExtension=".ex4" mimeType="application/octet-stream" />
      <mimeMap fileExtension=".ex5" mimeType="application/octet-stream" />
      <mimeMap fileExtension=".pagefind" mimeType="application/wasm" />
      <mimeMap fileExtension=".pf_meta" mimeType="application/wasm" />
      <mimeMap fileExtension=".pf_index" mimeType="application/wasm" />
      <mimeMap fileExtension=".pf_fragment" mimeType="application/wasm" />
    </staticContent>

    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" path="404.html" responseMode="File" />
    </httpErrors>

    <defaultDocument>
      <files>
        <clear />
        <add value="index.html" />
      </files>
    </defaultDocument>
  </system.webServer>

</configuration>
