简体   繁体   中英

Rewrite from local path to external domain - C# MVC IIS 7.5 URL

I'm having trouble getting url rewriting working in an MVC app in that I need to direct all requests from https://www.thisdomain.com/assets/ * to their equivalent at https://www.thatdomain.com/assets/ * eg

<link rel="stylesheet" href="/assets/css/app.css">

loads in the resources from https://www.thatdomain.com/assets/css/app.css

I've currently got this in the web.config

<system.webServer>
<rewrite>
    <rules>
    <rule name="Map Assets" stopProcessing="true">
    <match url="assets/(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^www.otherdomain.com/assets/$" />
    </conditions>
    <action type="Rewrite" url="/{R:1}" appendQueryString="true" />
    </rule>
</rules>
</rewrite>
</system.webServer>

and added this into my RouteConfig.cs

routes.IgnoreRoute("assets/");

Url Rewrite is installed on the server.

Any help would gladly be appreciated.

Some questions: Those "www.thisdomain.com" and "www.otherdomain.com", are hosted on the same server? Are you sure you want "Rewrite", not "Redirect"?

I am not even sure that it's possible to rewrite to another domain. You can rewrite a resource path or name, but domain makes not much sense to me. But Redirect could be configured as follows:

<rewrite>
    <rules>
    <rule name="Map Assets" stopProcessing="true">
    <match url="assets/(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="www.thisdomain.com" />
    </conditions>
    <action type="Redirect" url="www.otherdomain.com/assets/{R:1}" appendQueryString="true" />
    </rule>
</rules>
</rewrite>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM