简体   繁体   English

301重定向ASP.NET web.config包括参数

[英]301 Redirect in ASP.NET web.config including Parameters

I'm looking into using 301 redirects having noticed a bunch of hits on my domain on Google Analytics to .asp pages which not longer exist having moved everything over to a .NET setup. 我正在考虑使用301重定向,注意到我的域上的一些点击在Google Analytics上发送到.asp页面,这些页面已不再存在,已将所有内容移至.NET设置。

Having spent a bit of time Googling, I have been able to add the following code to my web.config. 花了一些时间谷歌搜索,我已经能够将以下代码添加到我的web.config。

<location path="products.asp">
    <system.webServer>
     <httpRedirect enabled="true" destination="https://www.hartnollguitars.co.uk/products.aspx" httpResponseStatus="Permanent" />
    </system.webServer>
</location> 

This is fine and moves everything from products.asp to pproducts.aspx but it does not preserve the querystring, which is essential to make any sense, ie products.aspx?id=789 这很好,并且将所有内容从products.asp移动到pproducts.aspx但它不保留查询字符串,这对任何意义都是必不可少的,即products.aspx?id=789

You have to add $Q to the destination url to preserve the querystring. 您必须将$Q添加到目标网址以保留查询字符串。 So in your case it should look like this: 所以在你的情况下它应该是这样的:

<location path="products.asp">
    <system.webServer>
     <httpRedirect enabled="true" destination="https://www.hartnollguitars.co.uk/products.aspx$Q" httpResponseStatus="Permanent" />
    </system.webServer>
</location> 

If you have IIS 7 or greater, there is a far more robust solution to URL rewrites. 如果您使用的是IIS 7或更高版本,则可以使用更强大的URL重写解决方案。 Using the URL Rewrite Module 2.0 , you can create rich redirects that can include the original query string. 使用URL Rewrite Module 2.0 ,您可以创建可以包含原始查询字符串的丰富重定向。 Following this guide on IIS.net , you can see in the screenshot the "Append query string" option. 按照IIS.net上的指南 ,您可以在屏幕截图中看到“附加查询字符串”选项。 IIS URL重写模块UI

You also have the option of using the server variable {QUERY_STRING}. 您还可以选择使用服务器变量{QUERY_STRING}。 Finally, if you used a Regex with groups in your rule's pattern, you can use the variables {R:#} as shown in the screenshot as well. 最后,如果您在规则模式中使用带有组的正则表达式,则可以使用变量{R:#},如屏幕截图所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM