我想将所有请求从 .com 重定向到 .net,并在 dot net core 中使用相同的路径和路由。
与以下从起始 URL 中删除 www 的代码相同:
app.UseRewriter(new RewriteOptions().Add(ctx =>
{
// checking if the hostName has www. at the beginning
var req = ctx.HttpContext.Request;
var hostName = req.Host;
if (hostName.ToString().StartsWith("www."))
{
// Strip off www.
var newHostName = hostName.ToString().Substring(4);
// Creating new url
var newUrl = new StringBuilder()
.Append(req.Scheme)
.Append(newHostName)
.Append(req.PathBase)
.Append(req.Path)
.Append(req.QueryString)
.ToString();
// Modify Http Response
var response = ctx.HttpContext.Response;
response.Headers[HeaderNames.Location] = newUrl;
response.StatusCode = 301;
ctx.Result = RuleResult.EndResponse;
}
}));