2

As a developer, I'd like to redirect certain (AJAX) requests to a DEV server rather than my local webserver.

For Example, redirect request for the subdirectory of "AbcService":

http://localhost/AbcService/json?queryString
http://dev.example.com/AbcService/json?queryString

I do not want to redirect other requests for localhost, e.g.:

http://localhost/XyzService/...

I thought I could do this with the IIS UrlRewrite 2.0 extension, but thus far I haven't been able to come up with a rule that works.

First, I'm not sure if I need a Rewrite or Redirect action. I can get the Redirect action to work, but no data is returned. Possibly authentication issues.

Here are the rules:

URL: *
Conditions:
 {HTTP_HOST} matches "localhost"
 {URL} matches "*/AbcService/*

Action:
Redirect (307)
URL: http://dev.example.com/AbcService/{C:2}

Where {C:2} is the path following "/AbcService/" from the {URL} condition.

One thing I don't understand is why switching this from a Redirect to Rewrite action results in a 404, even though I have the exact same conditions.

1 Answer 1

3

A few things:

A rewrite should be used if you want to transfer to a different URL within the same site. The IIS rewrite module can change the requested url internally and it can be processed in the same existing request pipeline.

A redirect is required when you want to transfer to a different site on the same server or a different server, which is your case.

When using a rewrite and also a absolute URL, I guess the module tries to resolve that within the existing site and can't, so it sends a 404.

Conditions are in addition to the matching URL, in your case you don't need them. Your condition {HTTP_HOST} is always true, it's your local dev-box and the name resolved to this site (unless you want it to behave differently when using 127.0.0.1). Condition {URL} should be moved into the pattern:

<rule name="ServerFault" patternSyntax="Wildcard" stopProcessing="true">
    <match url="AbcService/*" />
    <action type="Redirect" url="http://dev.example.com/AbcService/{R:1}" />
</rule> 

The match looks at everything after the host slash (first slash after the host name)

When posting Url Rewrite questions here, it's always a good idea to post the XML from the web.config

All that still doesn't explain why your redirect method does not work? Maybe a look through Fiddler may help.

1
  • Thanks Peter. I use Fiddler all the time so I had that mindset with specifying the host. I made the rule adjustments for your example. The issue is that the original request is a POST and upon redirect, it is switched to a GET.
    – jmmr
    Apr 16, 2015 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .