We have some third-party service on http://localhost:1234/, and we want to be able to access it externally via https://my-site.com/MyService/
For that we have IIS with Virtual Directory (Application) MyService and URL rewrite rule
<rule name="my-service" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<action type="Rewrite" url="http://localhost:1234/{R:0}" />
</rule>
This service renders html with some root urls like Page 1
But following such links will lead us to https://my-site.com/MyService/page1.html
In order to fix it we have another URL rewrite outbound rule
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img, Link, Script" pattern="^/(.*)" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="https://my-site.com/MyService/{R:1}" />
</rule>
And it works fine and renders absolute urls Page 1
However, if we have JavaScript
// redirect
location.href = "/page2.html";
// AJAX
var xhr = new XMLHttpRequest();
xhr.open("GET", "/page2.html");
xhr.send();
// fetch
fetch("/page2.html");
those root-based urls will be resolved as https://my-site.com/MyService/page2.html
As this service is third-party, I can't just go to modify all those JavaScript code to include /MyService prefix. And those urls such as /page2.html are constructed dynamically so it's impossible to just use URL rewrite module to fixup corresponding JavaScript.
I was trying to add URL rewrite to the root website to capture such urls via http referer
<rule name="MyService" stopProcessing="true">
<match url="^(?!MyService).+$" />
<conditions>
<add input="{HTTP_REFERER}" pattern="https://my-site.com/MyService*" />
</conditions>
<action type="Redirect" url="https://my-site.com/MyService/{R:0}" redirectType="Found" />
</rule>
but it still doesn't work properly and behaves very fragile.
Is there a way to solve such supposedly very common task easily?
Related
I am trying to do a custom domain mapping where in i have to map custom.seconddomain.com that is the custom domain and my app is hosted at app.firstdomain.com.
So i have added a CNAME entry to domain provider for seconddomain.com. Then added an IIS entry for custom.seconddomain.com in my server that hosts app.firstdomain.com. I have added a Rewrite rule in this IIS entry for the app routes to be rewritten to app.firstdomain.com. Its all well and good till this point.
<rule name="Subdomain rewrite" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^((http)?(s)?:\/\/)?custom.seconddomain.com\/(.*)" />
</conditions>
<action type="Rewrite" url="http://app.firstdomain.com/" appendQueryString="true" />
</rule>
But now since the assets and resources will only be available at app.firstdomain.com, its failing to load those assests on custom.seconddomain.com. So for that i added a rule on IIS Rewrite rule to redirect the assests request from custom.seconddomain.com to app.firstdomain.com, but not sure if its due to the precedence of the rules or something else. Both the rules can’t seem to operate together.
<rule name="Rule 1">
<match url="^((http)?(s)?:\/\/)?custom.seconddomain.com\/assets\/(.*)" />
<conditions>
<add input="{HTTP_REFERER}" pattern="^$" negate="true" />
</conditions>
<action type="Rewrite" url="https://app.firstdomain.com/assets/{R:4}" />
</rule>
I am new to IIS so was trying to achieve this through image hotlinking as well, to try and achieeve the desired outcome. Can you please let me know what am i missing here or doing wrong?
I am trying to redirect from non-www site to www and also from http to https.
Http to https works well, but non-www to www shows a page not found error.
This is my configuration:
<rules>
<clear />
<rule name="non-www to www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
</conditions>
<action type="Rewrite" url="https://www.{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />
</rule>
<rule name="http to https" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />
</rule>
</rules>
What is going on? I have tried several variants for non-www to www, all from sites found in google, but non worked.
Jaime
Copied from the comment with more explanation.
The action should be set to Redirect instead of Rewrite, because you do want search engines to show www links, and then your site must issue 301 redirection to their bots.
Even if you sometimes want Rewrite, the url must be a relative path to the same site. If rewriting to another site (like the one you used above), then ARR is required with proxy mode. Otherwise, IIS naturally returns 404 errors.
However, rewriting does not change URLs in browser address bar, so it also has no effect on search engine bots.
Reference
i have a site that used to use https://secure.example.com for all secure pages, now the entire site needs to be secure so now ANY url is secure under https://www.example.com
But have loads of legacy CMS content that isn't easy to manage so just wanted to put a rule in that ANY link that had https://secure.example.com/url.... rewritten to https://www.example.com/url....
Tried this
<rule name="secure to www" stopProcessing="true">
<match url="https://secure.example.com/(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^(secure)\.example\.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" />
</rule>
but if i try this - https://secure.example.com/resp_register.aspx?prod=41&sid=7337826962595
it fails
But this works and diverts to the secure https version
- http://secure.example.com/resp_register.aspx?prod=41&sid=7337826962595
I have multiple domains pointing to one hosting location.
I wish to establish one of the domains as my main domain and therefore I wish to perform a 301 redirect to this main domain whenever a user accesses my site from a secondary domain.
For example:
www.example.com
This is my main domain. I want all other domains associated with my site to redirect to here.
If a user comes in on:
www.test.com or
www.test.com/anypage
etc.
Then I want the user to be redirected to the example version of that page.
How do I do this using the web.Config file of my application? The reason I ask is that usually my web hosting provider has a tool in their back office that allows me to setup this redirect however, our client has opted for a different hosting provider that do not provide such a tool.
I have attempted to do this redirect using the following code but it doesn't seem to work:
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^test\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}}" redirectType="Permanent" />
</rule>
My application is an Umbraco powered site and so has several system.webServer entries in the web.config file. It may just be the case that I have entered this code in the wrong place but any help here would be greatly appreciated as I am only used to doing 301 redirects in .htaccess files.
This is not really that umbraco related but I think what you want to do is this:
<rewrite>
<rules>
<rule name="redirect" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Match all urls unless the host name part is exactly www.example.com - and redirect those to www.example.com/whatever.
How can I achieve this in IIS:
When I'll write in the browser: www.mydomain.com/mydirectory to go and point to subdomain.domain.com but the url to be like I've written it in my browser (eg.www.mydomain.com/mydirectory )
You can't rewrite a url to a different domain. You'd either have to do a redirect, or you can use an iframe to contain subdomain.domain.com inside of the page you have at www.mydomain.com/mydirectory.
If you want to rewrite subdomain.domain.com to something like domain.com/subdomain, this would be possible since it is the same domain name, here's the rule in web.config:
<rule name="RedirectSubdomain">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="(.*).domain.com" />
</conditions>
<action type="Rewrite" url="/{C:1}/{R:0}" />
</rule>