重定向 (web.config) 规则以强制执行子目录

Redirect (web.config) rule to enforce subdirectory

我们的网站使用 "subfolders" 来指定语言(例如 www.domain.com/nl 或 www.domain.com/en)。默认语言是荷兰语 (nl)。目前访问者都可以通过指定的语言(例如 www.domain.com/nl)或根域(例如 www.domain.com)访问页面。

出于 SEO 目的,我们希望将所有访问根目录的用户重定向(强制)到特定语言 (nl)(例如 www.domain.com/page/1 到 www.domain.com/nl/页/1)。

以前的方法(使用 web.config 重写规则)导致(无限)循环(例如 www.domain.com/nl/nl/nl/nl/nl/nl/nl/etc .).

我知道这个主题 Rewrite Rule to enforce default lang code in URL 描述了如何在 .htaccess 中执行此操作,但我不知道如何将其转换为 web.config。

提前致谢:-)

试试这个

   <configuration>
           <system.webServer>
             <rewrite>
<conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> <add input="{REQUEST_URI}" pattern="^/umbraco/" negate="true" /> <add input="{REQUEST_URI}" pattern="^/install/" negate="true" /> <add input="{REQUEST_URI}" pattern="^/nl/" negate="true" /> 
</conditions>
            <rules>   
             <rule name="Language" stopProcessing="true">
            <match url="^/nl" negate="true" />
            <action type="Redirect" url="/nl/{R:0}" redirectType="Permanent" />
            </rule>
            </rules>
            </rewrite>
            </system.webServer>
        </configuration>

以上规则会将所有 www.domain.com/sub/sub 重定向到 www.domain.com/nl/sub/sub