0

I've spent hours to find the correct syntax to replace the URLs of my old website for my new .NET Core3.0 version because Google indexed thousands of pages with old URLs.

I want to use URL Rewriting Middleware to do this for me:

old URL:

www.domain.com/details.aspx?u1=pagename

new URL:

www.domain.com/pagename

and also

old URL: www.domain.com/details.aspx?u1=pagename&u2=mode

new URL:

www.domain.com/pagename/mode

The code I tried is:

var rewriteOptions = new RewriteOptions()
                .AddRedirect("^.*(?:details.aspx).*$", "/{C:1}{C:2}", 301);

I know it is wrong, so can anyone help me how to use the Rules? Thanks.

1 Answer 1

1

Really wanted to get better at this myself so had some fun experimenting getting something to work. Sadly I had some troubling using .AddRedirect to also match on the querystring.

So I came up with the following solution, I hope this helps you:

var rewriteOptions = new RewriteOptions()
            .Add(new RuleForU1AndU2())
            .Add(new RuleForU1());

And then having 2 rules:

public class RuleForU1 : IRule
{
    private int StatusCode { get; } = (int)System.Net.HttpStatusCode.MovedPermanently;

    public void ApplyRule(RewriteContext context)
    {
        var request = context.HttpContext.Request;
        var url = request.Path.Value;
        var queryString = request.QueryString.Value;

        if (!url.Equals("/details.aspx", StringComparison.OrdinalIgnoreCase) || queryString.Length == 0)
        {
            context.Result = RuleResult.ContinueRules;
            return;
        }

        var regexMatches = Regex.Matches(queryString, @"^?u1=(.*)");
        if(regexMatches.Count == 0)
        {
            context.Result = RuleResult.ContinueRules;
            return;
        }

        string newUrl = regexMatches[0].Groups[1].Value;

        var response = context.HttpContext.Response;
        response.StatusCode = StatusCode;
        response.Headers[Microsoft.Net.Http.Headers.HeaderNames.Location] = newUrl;
        context.Result = RuleResult.EndResponse;
        return;
    }
}

public class RuleForU1AndU2 : IRule
{
    private int StatusCode { get; } = (int)System.Net.HttpStatusCode.MovedPermanently;

    public void ApplyRule(RewriteContext context)
    {
        var request = context.HttpContext.Request;
        var url = request.Path.Value;
        var queryString = request.QueryString.Value;

        if (!url.Equals("/details.aspx", StringComparison.OrdinalIgnoreCase) || queryString.Length == 0)
        {
            context.Result = RuleResult.ContinueRules;
            return;
        }

        var regexMatches = Regex.Matches(queryString, @"^?u1=(.*)&u2=(.*)");
        if (regexMatches.Count == 0)
        {
            context.Result = RuleResult.ContinueRules;
            return;
        }

        string newUrl = $"{regexMatches[0].Groups[1].Value}/{regexMatches[0].Groups[2]}";

        var response = context.HttpContext.Response;
        response.StatusCode = StatusCode;
        response.Headers[Microsoft.Net.Http.Headers.HeaderNames.Location] = newUrl;
        context.Result = RuleResult.EndResponse;
        return;
    }
}

I didn't really put any effort into code reuse... and you can probably merge the 2 rules into one rule, but I don't know what your full usecase is (like will there also be a u3?) So... I hope this helps you get closer to where you need to go :)

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, I accepted your reply, because it solved my problem. But it's really strange to me why there is no Conditions and Actions available in RewriteOptions. When using IIS URLRewriteModule, using different conditions and variables makes it too easy to do complicated URL rewrite rules where the Microsoft.AspNetCore.Rewrite is somehow useless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.