【问题标题】:How to rewrite URL in multi-lingual ASP.NET website如何在多语言 ASP.NET 网站中重写 URL
【发布时间】:2015-05-18 03:12:13
【问题描述】:

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

我有如下网址: http://example.com/lang/en/index.htmlhttp://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'。

如何实现这个目标?

【问题讨论】:

    标签: asp.net url-rewriting


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

    【讨论】:

    • 我试过如果我打开example.com/en/index - “您要查找的资源已被删除、更改名称或暂时不可用”
    • 地址是否如您所愿改变?我不知道 example.com/en/index 将重定向到哪里我可以帮助您(并且您要求)将 example.com/lang/en/index 重写为 example.com/en/index
    • 不,它没有。此路径:“/lang/en/index.html”它是物理文件夹和文件路径。我只需要隐藏“lang”和“.html”。所以如果我去'example.com/en/index'它应该打开这个'example.com/lang/en/index.html'
    • 如果我选择 'example.com/en/index' 或 'example.com/en/index.html' - “您要查找的资源已被删除,名称已更改,或者暂时不可用”
    • 能否告诉我真实的域名以便我自己查看?
    【解决方案2】:

    [更新] 终于解决了。这是 Web.config:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <system.web>
        <compilation debug="false" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
        <customErrors mode="On" />
      </system.web>
    <system.webServer>
        <rewrite>
          <rules>
            <rule name="Ignore" enabled="true" stopProcessing="true">
              <match url="^(js|css).*" />
              <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
              <action type="None" />
            </rule>
            <rule name="Redirect requests to friendly URLs">
              <match url="^(.*?)/(.*)\.html$" />
              <action type="Redirect" url="{R:2}" />
            </rule>
            <rule name="Rewrite friendly URLs to phsyical paths">
             <match url="^\/(?!#.*).*" />
             <action type="Rewrite" url="lang/{R:0}.html" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    

    特别感谢 Ashkan Mobayen Khiabani 的“忽略”部分(需要忽略:javaScript、图像和 CSS 文件夹以保持链接正常工作)

    【讨论】:

      猜你喜欢
      • 2010-11-19
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 2010-09-16
      • 2013-12-23
      • 1970-01-01
      相关资源
      最近更新 更多