如何将RedirectType添加到asp.net中的外部配置文件 [英] How to add RedirectType to External config file in asp.net
问题描述
我在包含Mapped URLS for redirect
的应用程序根目录中有一个单独的.config
文件,并在web.config
中为301 Permanent Redirect
引用了该.config
文件!这很好.
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
现在,我还想添加一些链接,这些链接将重定向为302状态代码.如何在外部.config文件中添加302重定向并进行相应的重定向.
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>
我们可以在此文件中指定重定向类型,即301/302吗?
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>
注意:当前,文件'rewritemaps.config'
中的所有链接都已设置为web.config
中的301 Status
.
NOTE:Currently all links from file 'rewritemaps.config'
are set to 301 Status
in web.config
.
我们可以在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)
中无法实现,则请建议首选方法?
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?
更新: 如果请求的URL中的特定字符串匹配,可以帮助我重定向到另一个站点(不同的域). 例如:如果所请求的URL包含"/hm1",则重定向到其他站点.即 http://www.google.com
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>
推荐答案
可以将RedirectType
添加到重写映射中吗?不,不幸的是没有.
Can you add RedirectType
into a Rewrite Map? No, unfortunately not.
要实现您要执行的操作,您将需要创建两个重写映射和两个重写规则-一个用于301重定向,一个用于302重定向.
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>
这篇关于如何将RedirectType添加到asp.net中的外部配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!