How to add RedirectType to External config file in asp.net

感情迁移 提交于 2019-12-11 19:30:26

问题


I have a separate .config file in the application root directory which contains Mapped URLS for redirect and referenced this .config file in web.config for 301 Permanent Redirect! This works fine.

See Reference Link

Now, i also want to add some links which will redirected as 302 status code. How to add 302 redirect in external .config file and redirect accordingly.

rewritemaps.config

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

Can we specify the Redirect Type i.e 301/302 in this file?

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>

NOTE:Currently all links from file 'rewritemaps.config' are set to 301 Status in web.config.

Can we add as following in rewritemaps.config and redirect accordingly:

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

There are about 1000 links of 301 Status and about 400 links for 302 Status. If its not possible in external file(rewritemaps.config) then please suggest preferred way to do?

Update: Can you help me to redirect to another site(different domain) if specific string match in requested URL . Eg: if the requested URL contains "/hm1" then redirect to different site. i.e 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>

回答1:


Can you add RedirectType into a Rewrite Map? No, unfortunately not.

To achieve what you're trying to do you're going to need to create two Rewrite Maps and two Rewrite Rules - one for 301 redirects and one for 302 redirects.

Here's an example of how that might look:

<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>


来源:https://stackoverflow.com/questions/27148725/how-to-add-redirecttype-to-external-config-file-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!