【问题标题】:Redirect (web.config) rule to enforce subdirectory重定向 (web.config) 规则以强制执行子目录
【发布时间】:2017-11-15 08:05:56
【问题描述】:

我们的网站使用“子文件夹”来指定语言(例如 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/page/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。

提前致谢:-)

【问题讨论】:

  • 所以你希望所有进入 www.dowmain.com/page/1 的用户都被重定向到 www.domain.com/nl/page/1 ?
  • @ISHIDA 正确。和 www.domain.com/sub/sub/sub 到 www.domain.com/nl/sub/sub/sub。所以总是强制执行语言子。

标签: .net web-config


【解决方案1】:

试试这个

   <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

【讨论】:

  • 谢谢。不幸的是,这不起作用。以前试过这个。例如,当访问 domain.com/page 时,它​​不会重定向到 domain.com/nl/page,而是重定向到 domain.com/nl。
  • 导致无限循环(例如 /nl/nl/nl/nl/nl)
  • 让它在这些条件下工作。谢谢!
猜你喜欢
  • 1970-01-01
  • 2019-08-02
  • 2020-08-14
  • 2015-01-26
  • 2012-05-07
  • 1970-01-01
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多