URL Rewrite

Jul 20, 2011 at 5:33 PM

Is it possible to do URL rewrites with this web server?  I am using it now and it is working great, but I want to also create a REST API and need to be able to to do URL rewrites.  Any info would be appreciated. 

Jul 20, 2011 at 5:49 PM

You can use routers.


They can either send redirect responses or just change the uri in the request to anything you like.

Jul 20, 2011 at 8:04 PM

Thanks jgauffin.

Would you mind explaining a little how I would go about getting this working.  I want users to be able to access my api in the following way:  myipaddress.com/api/runCommand/123   I then want this request to get rewritten to myipaddress.com/api/api.php in order to process the api call.  How can I do this using routers like you suggest?

Aug 2, 2011 at 10:35 PM
Edited Aug 3, 2011 at 8:24 AM

The RegExRouter is exactly what you're looking for:

You can use a regular expression like /api/(?<cmd>[a-zA-Z0-9]+)/(?<arg>[a-zA-Z0-9]+) and then use the named captures, so you get something like 


IRouter router = new RegExRouter(
    "/api/(?<cmd>[a-zA-Z0-9]+)/(?<arg>[a-zA-Z0-9]+)", // from
    "http://myipaddress.com/api.php?cmd=${cmd}&arg=${arg}", // to
    false); // do not redirect, just rewrite the request uri