asp.net - 如何在多语言 ASP.NET 网站中重写 URL

标签 asp.net url-rewriting

我想使用带规则部分的 Web.config 重写我的 asp.net 多语言网站中的 URL。

我有这样的网址: http://example.com/lang/en/index.html, http://example.com/lang/fr/index.html

我需要删除 lang.html 扩展名并将 url 重写为: http://example.com/en/index, http://example.com/fr/index

我的 Web.config:

<system.webServer>
  <rewrite>
    <rules>
        <rule name="RewriteHTML">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="{R:1}.html" />
        </rule> 
    <rule name="Rewrite friendly URLs to phsyical paths">
     <match url="^(.*)$" />
     <action type="Rewrite" url="lang/{R:0}" />
    </rule>
    </rules>
  </rewrite>
 </system.webServer>

所以如果我去 ' http://example.com/en/index ' 我需要打开此页面:' http://example.com/lang/en/index.html '.

如何实现这个目标?

最佳答案

<rule name="the name" enabled="true" stopProcessing="true">
          <match url="example.com/(.+)(?:/(.+)?)(.html)?"/>
          <action type="Rewrite" url="lang/{R:1}/{R:2}.html"/>
</rule>

关于asp.net - 如何在多语言 ASP.NET 网站中重写 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30294528/

相关文章:

c# - 为什么我的 ASP.NET Web 服务不能启动进程,但我的 .NET 控制台应用程序可以?

c# - .net c# sql 字符串到日期转换错误

c# - Visual Studio 2019 中没有 Razor 组件模板

redirect - 如果端口 80 关闭,我可以从 HTTP 重定向到 HTTPS 吗?

azure - URL 中出现 5 个字符

php - 重写规则导致403禁止错误

asp.net - 如何在 ASP.NET Web 窗体中使用依赖注入(inject)

c# - 如何在 Asp.net 表中从左到右更改数据绑定(bind)?

java - Struts 2中tuckey出站规则中&amp的使用方法

java - trimFilter和UrlRewriteFilter导致IllegalStateException