gpt4 book ai didi

c# - ASP.NET 如何在路径和查询完好无损的情况下对新域执行 HTTP 301

转载 作者:可可西里 更新时间:2023-11-01 17:13:40 26 4
gpt4 key购买 nike

我怎样才能将所有传入请求从一个域重定向到另一个域并仍然保留路径和查询?

例子来自:http://domain1.com/some/path/?query至:http://domain2.com/some/path/?query .

我一直在 web.config、HTTP-handlers 和 global.asax 中的 system.webserver 闲逛 - 但没有运气。我只得到 404s(因为内容已被移动)...

谢谢!

最佳答案

使用Request.RawUrl并在进行重定向时将 domain1 替换为 domain2。

来自备注部分:

The raw URL is defined as the part of the URL following the domain information. In the URL string http://www.contoso.com/articles/recent.aspx, the raw URL is /articles/recent.aspx. The raw URL includes the query string, if present.

更新:

这个:

protected void Application_BeginRequest(object sender, EventArgs e)
{
Response.Redirect("http://www.google.com" + Request.RawUrl);
}

绝对按您的需要工作。您可能会收到 404 错误,但这只是因为 Url 的路径部分在 domain2 上不存在(上例中的 google.com)。这是您应该能够预测/纠正或根本不用担心的事情。我不知道你的要求是什么。

关于c# - ASP.NET 如何在路径和查询完好无损的情况下对新域执行 HTTP 301,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8333426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com