Reputation: 3067
Google is getting confused about which page to index, even though it is all nicely explained in the sitemap.
I created a redirect rule in web.config to redirect traffic from naked to www and this works great
<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>
I am trying to create a second rule to redirect domain/home/index and domain/index to root but am not able to. Below doesn't work. I tried a few variations with different regex in pattern fields, but nothing is working.
<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>
Upvotes: 2
Views: 5682
Reputation: 601
In ASP .NET Core 3.1 I have used successfully quite simple lines of code to redirect non-www to www by utilizing:
using Microsoft.AspNetCore.Rewrite;
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// redirect non-www to www website
app.UseRewriter(new RewriteOptions()
.AddRedirectToWww()
);
RewriteOptions
should have possible list of custom rules as mentioned in another posts.
I refer to the official Microsoft Doc URL Rewriting Middleware in ASP.NET Core where is explained in details.
Upvotes: 2
Reputation: 2091
In ASP.NET Core(MVC6), we can use custom Middle Ware
to achieve this.
Here is a simple demo for your reference.
First, create a Middle Ware
class. According to your requirement, i have created it for you as below:
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);
}
}
}
}
Then we can use it in Configure
method in Startup
class as below:
I have tested it and it worked. Hope this would be helpful.
Upvotes: 3
Reputation: 3067
This did the trick. This converts URL to lowercase, redirects domain/home/index and domain/home to domain and domain/home/something to domain/something. Also adds www to naked domain.
Perhaps this should be a default behaviour?
Please let me know if there's a better way
<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>
Upvotes: 1