【问题标题】:URL Rewrite language querystringURL 重写语言查询字符串
【发布时间】:2023-03-04 16:34:01
【问题描述】:

我想为所选语言重写我的查询字符串。

我有这个网址:www.example.com/?lang=en,并希望它是 www.example.com/en

它应该在所有页面上重写。所以 www.example.com/contact.aspx?lang=en 将是 www.example.com/en/contact.aspx

是否有一个通用的重写规则?

【问题讨论】:

    标签: iis-7 url-rewriting


    【解决方案1】:

    这个有效。

    <rule name="Rewrite Language">
        <match url="([a-z]{2})(.*)" />
        <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:2}?lang={R:1}" />
    </rule>
    

    【讨论】:

      【解决方案2】:

      我的语法可能有点不对,但你可以这样做:

      <rewrite>
        <rules>
          <rule name="Rewrite Language">
            <match url="/([a-z]+)/([_0-9a-z-]+)" />
            <action type="Rewrite" url="{R:2}?lang={R:1}" />
          </rule>
        </rules>
      </rewrite>
      

      【讨论】:

      • 这似乎不起作用。通过 example.com/en/contact.aspx 给我 404
      • 我还没有玩过 URL 重写,但是动作标签的 url 前面不需要前导 '/' 来匹配匹配标签 url 中的前导 '/' 吗?
      【解决方案3】:

      请考虑使用以下内容:

      <rewrite>
        <rules>
          <rule name="Rewrite Language">
            <match url="/([a-z]{2})(.*)" />
            <action type="Rewrite" url="{R:2}?lang={R:1}" />
          </rule>
        </rules>
      </rewrite>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-02
        • 2012-01-18
        • 1970-01-01
        • 2014-08-07
        • 2019-02-13
        • 1970-01-01
        相关资源
        最近更新 更多