Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom URL Rewriting

I want to rewrite a url which is simple by using the UseRewriter Middleware.

var rewrite = new RewriteOptions()
    .AddRedirect("Page1From", "Page1To")        // Redirect
    .AddRewrite("Page2From", "Page2To", true);  // Rewrite

app.UseRewriter(rewrite);

This will have as a result, the url "/Page1From" will be redirected to "Page1To" and the url "Page2From" will display the contents of the "/Page2To" without redirect.

I want to implement the AddRewrite method by using data from database but I only found how to redirect using a custom Rule.

var rewrite = new RewriteOptions()
    .AddRedirect("Page1From", "Page1To")        // Redirect
    .AddRewrite("Page2From", "Page2To", true);  // Rewrite
    .Add(new MoviesRedirectRule(                // Custom Rule
        matchPaths: new[] { "/Page3From1", "/Page3From2", "/Page3From3" },
        newPath: "/Page3To"));

app.UseRewriter(rewrite);

and the rule is the following:

public class MoviesRedirectRule : IRule
{
    private readonly string[] matchPaths;
    private readonly PathString newPath;

    public MoviesRedirectRule(string[] matchPaths, string newPath)
    {
        this.matchPaths = matchPaths;
        this.newPath = new PathString(newPath);
    }

    public void ApplyRule(RewriteContext context)
    {
        var request = context.HttpContext.Request;

        // if already redirected, skip
        if (request.Path.StartsWithSegments(new PathString(this.newPath)))
        {
            return;
        }

        if (this.matchPaths.Contains(request.Path.Value))
        {
            var newLocation = $"{this.newPath}{request.QueryString}";

            var response = context.HttpContext.Response;
            response.StatusCode = StatusCodes.Status302Found;
            context.Result = RuleResult.EndResponse;
            response.Headers[HeaderNames.Location] = newLocation;
        }
    }
}

This will redirect the following urls:

  • /Page3From1
  • /Page3From2
  • /Page3From3

to /Page3To

I want to create something similar which will not redirect but I want to Rewrite the url so that the url will remain the same but it will display the contents of a specified URL.

Can someone tell me what changes I have to do to the response object in order to make it work?

like image 327
pitaridis Avatar asked Mar 06 '18 13:03

pitaridis


1 Answers

RewriteRule that is added when you call AddRewrite() just replaces URI parts in request (link to source code):

request.Scheme = scheme;
request.Host = host;
request.Path = pathString;
request.QueryString = query.Add(request.QueryString);

In your case, since you are replacing only Path part, ApplyRule method would be as simple as:

public void ApplyRule(RewriteContext context)
{
    var request = context.HttpContext.Request;

    if (matchPaths.Contains(request.Path.Value))
    {
        request.Path = newPath;
        context.Result = RuleResult.SkipRemainingRules;
    }
}
like image 130
CodeFuller Avatar answered Sep 19 '22 13:09

CodeFuller