【问题标题】:How to add RedirectType to External config file in asp.net如何将 RedirectType 添加到 asp.net 中的外部配置文件
【发布时间】:2015-01-24 18:08:25
【问题描述】:

我在应用程序根目录中有一个单独的.config 文件,其中包含Mapped URLS for redirect,并在web.config 中为301 Permanent Redirect 引用了这个.config 文件!这工作正常。

See Reference Link

现在,我还想添加一些将重定向为 302 状态代码的链接。如何在外部 .config 文件中添加 302 重定向并进行相应的重定向。

rewritemaps.config

<rewriteMaps>
    <rewriteMap name="Redirects">
       <add key="/oldcellphone" value="/newcellphones.aspx" />
    </rewriteMap>
</rewriteMaps>

我们可以在这个文件中指定重定向类型,即 301/302 吗?

web.config

<system.webServer>
     <rewrite>
      <rewriteMaps configSource="rewritemaps.config">
        </rewriteMaps>
          <rules>
            <rule name="Redirect rule1 for Redirects">
              <match url=".*" />
              <conditions>
                <add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
              </conditions>
              <action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent"/>
            </rule>
          </rules>
        </rewrite>
    </system.webServer>

注意:目前来自文件'rewritemaps.config' 的所有链接都设置为web.config 中的301 Status

我们能否在 rewritemaps.config 中添加如下内容并进行相应的重定向:

<add key="/oldcellphone" value="/newcellphones.aspx" [RedirectType=301] />
<add key="/oldphone" value="/newphones.aspx" [RedirectType=302] />

大约有1000 links of 301 Status 和大约400 links for 302 Status。如果在external file(rewritemaps.config) 中不可能,那么请建议首选方法?

更新: 如果请求的 URL 中的特定字符串匹配,你能帮我重定向到另一个站点(不同的域)吗? 例如:如果请求的 URL 包含“/hm1”,则重定向到不同的站点。即http://www.google.com

Web.config

<rule name="othersite" stopProcessing="true">
<match url="^/hm1$" />
<action type="Redirect" url="http://www.google.com" redirectType="Found"/>
</rule>

.aspx

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="/hm1">other site (http://www.google.com)</asp:HyperLink>

【问题讨论】:

    标签: redirect web-config http-status-code-301 http-status-code-302


    【解决方案1】:

    您可以将RedirectType 添加到重写映射中吗?不,很遗憾没有。

    为了实现你想要做的事情,你需要创建两个重写映射和两个重写规则——一个用于 301 重定向,一个用于 302 重定向。

    下面是一个例子:

    <rewrite>
      <rules>
        <rule name="301Redirects" stopProcessing="true">
          <match url=".*" />
          <action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
          <conditions>
            <add input="{301Redirects:{REQUEST_URI}}" pattern="(.+)" />
          </conditions>
        </rule>
        <rule name="302Redirects" stopProcessing="true">
          <match url=".*" />
          <action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Found" />
          <conditions>
            <add input="{302Redirects:{REQUEST_URI}}" pattern="(.+)" />
          </conditions>
        </rule>
      </rules>
      <rewriteMaps>
        <rewriteMap name="301Redirects">
          <add key="/oldurl" value="/newurl" />
        </rewriteMap>
        <rewriteMap name="302Redirects">
          <add key="/oldcellphone" value="/newcellphones.aspx" />
        </rewriteMap>
      </rewriteMaps>
    </rewrite>
    

    【讨论】:

    • 嗨@Tom Hall,我会再试一个问题,如果请求的URL 中的特定字符串匹配,我们可以重定向到其他域吗?请查看帖子中的“更新”部分并指导我..!
    • 是的,你可以重定向到任何你想要的地方。
    • 谢谢!是的,这行得通!但是我重定向到其他网站将不起作用,你会帮助我吗?请查看我添加的规则并帮助我..?我尝试使用上面“更新”部分中提到的“规则”重定向到另一个站点,但它不起作用..!
    • @TomHall 我如何将这些apache 规则翻译成iis (web.config) --> sitepoint.com/community/t/… 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多