301在Azure ASP.Net Core网站中重定向

时间:2018-08-25 23:09:34

标签: asp.net-mvc azure web-config url-redirection

即使站点地图中已对所有页面进行了很好的说明,Google也对要索引的页面感到困惑。

我在web.config中创建了重定向规则,以将流量从裸露重定向到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>

3 个答案:

答案 0 :(得分:1)

这成功了。 这会将URL转换为小写,将domain / home / index和domain / home重定向到domain,然后将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>

答案 1 :(得分:1)

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

这是一个简单的演示供您参考。

首先,创建一个Middle Ware类。根据您的要求,我为您创建了以下内容:

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);
            }

        }
    }
}

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

enter image description here

我已经对其进行了测试,并且可以正常工作。希望这会有所帮助。

答案 2 :(得分:0)

在 ASP .NET Core 3.1 中,我成功地使用了非常简单的代码行,通过利用:

using Microsoft.AspNetCore.Rewrite;

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {

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

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

我参考了官方 Microsoft Doc URL Rewriting Middleware in ASP.NET Core,其中有详细说明。