I want to redirect all request from .com to .net with the same path and route in dot net core.
Same as below code for removing www from start URL:
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;
}
}));
You can change your existing code to change the com to net.
if (hostName.ToString().EndsWith(".com"))
{
// change the com to net
var newHostName = hostName.ToString().Substring(0, hostName.ToString().Length - 4) + ".net";
// Creating new url
:
:
}
I have not tested it but it should work.