Jonathan
Jonathan

Reputation: 15462

URL rewrite causing 404

I've set up a simple rule to rewrite all requests to my domain:

<rule name="Rewrite Test" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="mydomain.com" />
    </conditions>
    <action type="Rewrite" url="http://mydomain.com/TestImage.jpg" appendQueryString="false" />
</rule>

With this enabled I receive a 404 error every time I access my domain. However if I change this to a redirect rule, everything works fine. Is there something I'm missing here?

Thanks in advance.

Upvotes: 2

Views: 7024

Answers (1)

Marco Miltenburg
Marco Miltenburg

Reputation: 6138

As Daniel explains, you can't rewrite to a domain (unless you set up IIS as a reverse proxy with ARR). You can only rewrite to another URL on the same site and thus it will implicitly be rewritten the same domain that the original request was to.

Your rewrite rule should be:

<rule name="Rewrite Test" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="mydomain.com" />
    </conditions>
    <action type="Rewrite" url="/TestImage.jpg" appendQueryString="false" />
</rule>

If the site is not bind to other domain names that serve up other content you could potentially also drop the condition of the rule and make it even simpler:

<rule name="Rewrite Test" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*" />
    <action type="Rewrite" url="/TestImage.jpg" appendQueryString="false" />
</rule>

A redirect works fine with your example as you can (of course) redirect the client to a URL on the same domain but also to a URL on another domain.

Upvotes: 5

Related Questions