目标:尝试删除www。从网址(例如http://www.myurl.com/r/reddit/other到http://myurl.com/r/reddit/other)
什么坏了?网址重写无法将其重定向到非www网站吗?
会发生什么?一切正常,但可以正常编译,但www网址 挂起直到连接超时,并且不会按照代码的意图重定向
这是我的代码。我不确定发生了什么,但不确定此应用程序的规格:
Net Core 2.0 使用Net Core 2.0.9运行时
规则
public class WWWRule : IRule
{
public void ApplyRule(RewriteContext context)
{
HttpRequest request = context.HttpContext.Request;
if (request.Host.Value.StartsWith("www.", StringComparison.OrdinalIgnoreCase))
{
HttpResponse response = context.HttpContext.Response;
var newhost = request.Host.Value.Substring(4);
string redirectUrl = $"{request.Scheme}://{newhost}{request.Path}{request.QueryString}";
response.Headers[HeaderNames.Location] = redirectUrl;
response.StatusCode = StatusCodes.Status301MovedPermanently;
context.Result = RuleResult.EndResponse;
}
}
}
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
RewriteOptions rewriteOptions = new RewriteOptions().Add(new WWWRule());
app.UseRewriter(rewriteOptions);
}
答案 0 :(得分:0)
想通了。
记住要在DNS记录中实际注册子域。 (CNAME,www,yourServerName)