Azure ASP.Net 核心网站中的 301 重定向

301 redirect in Azure ASP.Net Core website

Google 对要索引哪个页面感到困惑,尽管站点地图中对此进行了很好的解释。

我在 web.config 中创建了一个重定向规则来将流量从 naked 重定向到 www,这非常有效

 <rule name="Redirect to www" patternSyntax="Wildcard" stopProcessing="true">
        <match url="*" />
        <conditions>
          <add input="{HTTP_HOST}" pattern="example.com" />
        </conditions>
        <action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" />
      </rule>

我正在尝试创建第二条规则以将 domain/home/index 和 domain/index 重定向到 root,但我无法做到。下面不行。我在模式字段中尝试了一些使用不同正则表达式的变体,但没有任何效果。

    <rule name="Redirect index" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="example.com/home/index" />
      </conditions>
      <action type="Redirect" url="https://www.example.com/" redirectType="Permanent" />
    </rule>

这成功了。 这会将 URL 转换为小写,将 domain/home/index 和 domain/home 重定向到域,将 domain/home/something 重定向到 domain/something。还将 www 添加到裸域。

也许这应该是默认行为?

如果有更好的方法请告诉我

<rewrite>
  <rules>
    <rule name="Convert to lower case" stopProcessing="false">
      <match url=".*[A-Z].*" ignoreCase="false" />
      <action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
    </rule>
    <rule name="Redirect index" patternSyntax="Wildcard" stopProcessing="true">
      <match url="home/index" />
      <action type="Redirect" url="https://www.example.com" redirectType="Permanent" />
    </rule>
    <rule name="Redirect home" patternSyntax="Wildcard" stopProcessing="true">
      <match url="home" />
      <action type="Redirect" url="https://www.example.com" redirectType="Permanent" />
    </rule>
    <rule name="Redirect home2" patternSyntax="Wildcard" stopProcessing="true">
      <match url="home/*" />
      <action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
    </rule>
    <rule name="Redirect to www" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^example.com" />
      </conditions>
      <action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" />
    </rule>
  </rules>

在ASP.NET Core(MVC6)中,我们可以使用自定义Middle Ware来实现。

这里有一个简单的demo,供大家参考。

首先,创建一个Middle Wareclass。根据您的要求,我为您制作如下:

using ASPNETCore.Test;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ASPNETCore.Middleware
{
    public class MyMiddleware
    {
        private RequestDelegate _nextDelegate;
        private IServiceProvider _serviceProvider;

        public MyMiddleware(RequestDelegate nextDelegate, IServiceProvider serviceProvider)
        {
            _nextDelegate = nextDelegate;
            _serviceProvider = serviceProvider;        
        }

        public async Task Invoke(HttpContext httpContext)
        {
            string requestURL = httpContext.Request.Path.ToString().ToLower();

            //redirect domain/home/index and domain/home to domain
            if (requestURL.Contains("/home/index")||requestURL.EndsWith("/home"))
            {
                httpContext.Response.Redirect("/");
            }
            // redirect domain/home/something  to domain/something
            else if (requestURL.Contains("/home/"))
            {
                Regex reg = new Regex("/home/(.+)");
                Match match = reg.Match(requestURL);
                string value = match.Groups[1].Value;
                httpContext.Response.Redirect("/"+ value);
            }else{

                await _nextDelegate.Invoke(httpContext);
            }

        }
    }
}

然后我们可以在Startup class中的Configure方法中使用它,如下所示:

我已经测试过它并且有效。希望这会有所帮助。

在 ASP .NET Core 3.1 中,我已经成功地使用非常简单的代码行将非 www 重定向到 www,方法是:

using Microsoft.AspNetCore.Rewrite;

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {

        // redirect non-www to www website 
        app.UseRewriter(new RewriteOptions()
            .AddRedirectToWww()
        );

RewriteOptions 应该有其他帖子中提到的可能的自定义规则列表。

我参考了微软官方文档URL Rewriting Middleware in ASP.NET Core,里面有详细的解释。