6

In my asp.net core app (angular 4 front end) I accept a URL like this:

example.com/report;url=http%3A%2F%2Fexample2.com

I would like to create a rewrite rule that allowed people to enter the following url:

example.com/report;url=http://example2.com

I can't find out how to do this.

I tried:

var options = new RewriteOptions()
    .AddRewrite(@"(.*);url=http:\/\/([^;]*)(.*)", "$1;url=http%3A%2F%2F$2$3", skipRemainingRules: false)
    .AddRewrite(@"^report.*", "index.html", skipRemainingRules: true)

app.UseRewriter(options);

This didn't work but even if it did it wouldn't account for urls that have slashes after the domain, i.e. sub directories. Using a group matching pattern I think it's impossible to do that. It needs to be a find & replace type operation on matched group.

Other webservers have this as a configurable option to decode slashes. I can't find any reference to it in the asp.net core docs. Is this possible?

Guerrilla
  • 13,375
  • 31
  • 109
  • 210
  • AFAIK you have to use a reverse proxy between your .net-core app and the Internet. I used Nginx for that. Have you tried to do rewriting there? I think it should work. BTW, using directories as something else is not a good idea, unless they are related to your MVC structure, but then you wouldn't use rewriting, you would match patterns to the controllers (or for simple CMS, use 1 controller to match everything and then process the URL-s within the controller). If using MVC you can return `Redirect` response when needed. – Harry Apr 04 '17 at 16:21
  • Where do you host your app? IIS, nginx, self-hosting? – Evk Apr 04 '17 at 20:18
  • 5
    Why can't you use query string to actually hold the parameters i.e. use `?` and change your URL to `example.com/report?url=http://example2.com`? And then you probably will not have a need for any URL re-write at all. – SergGr Apr 05 '17 at 00:45
  • @SergGr I don't get your point. The url is already a query param – Guerrilla Apr 11 '17 at 14:40
  • @Guerrilla, if your URL is actually exactly as in your example, then no. According to [RFC 3986](https://tools.ietf.org/html/rfc3986#section-3) `query` is the part after `?` (question mark) and this is what most web MVC frameworks expect when they perform routing and arguments parsing. P.S. This seems to be the same as what KyleB suggests – SergGr Apr 11 '17 at 15:57

2 Answers2

0

You're going to want to pass a parameter for the URL. There really is no way to get you what you want by allowing the user to enter a URL as a parameter in the address bar. It will always need to be encoded.

Instead of:

http://example.com/report;url=http%3A%2F%2Fexample2.com

use:

http://example.com/report?url=http%3A%2F%2Fexample2.com

Rather than having the user enter all of this into a browser address bar, I would instead create a user interface that allows a user to ask for a report and hit submit. The report would need a textbox for the URL and it will send a get request to your site after encoding the contents of the URL textbox into the 'url' parameter.

Using a URL re-write module is probably going against the grain here.

Kyle B
  • 2,328
  • 1
  • 23
  • 39
  • I am of course passing the params within my application. That is not the issue. In other webservers it is possible to decode the slashes in the url to allow links like I requested. My question is about how to do this in kestrel/asp.net core. – Guerrilla Apr 11 '17 at 14:40
  • I am using angular routing so i need to use the matrix query param style – Guerrilla Apr 11 '17 at 14:42
0

Unfortunately based on the code of the RewriteRule class, it is not possible. You should be able to create your own custom rewrite rule though which would implement IRule interface and URL encode part of your request path.

The source code of the RewriteRule can be found here https://github.com/aspnet/BasicMiddleware/blob/dev/src/Microsoft.AspNetCore.Rewrite/Internal/RewriteRule.cs

And here is the UrlEncoder to encode the value of the 'url' https://github.com/dotnet/corefx/blob/master/src/System.Text.Encodings.Web/src/System/Text/Encodings/Web/UrlEncoder.cs

Eugene Komisarenko
  • 1,533
  • 11
  • 25