Url rewrite first path segment to subdomain - redirect

I'm trying to do the below with a rewrite. From my attempts so far, it doesn't appear possible since I don't think I have access to rewrite the host.
domain.com/sub/web/mypage needs to become sub.domain.com/web/mypage
Essentially trying to do the opposite of this: URL Rewrite city.domain.com to domain.com/city
Was hoping to figure out a rewrite or something so I don't have to spend weeks fixing routes in a legacy webforms application that's very coupled to the expected subdomain url pattern.
My thought was to rewrite on a proxy server and send to the server farm with the "old" subdomain url and it would maybe "just work".

Do you want to redirect from https://doman.com/sub/web/mypage to https://sub.domain.com/web/mypage? if so, you can try this rule:
<rule name="test1" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain.com$" />
<add input="{HTTPS}" pattern="on" />
<add input="{REQUEST_URI}" pattern="^/sub/web/mypage" />
</conditions>
<action type="Rewrite" url="https://sub.domain.com/web/mypage" logRewrittenUrl="true" />
</rule>

Related

Custom domain and assets redirection using IIS Rewrite

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?

IIS URL Rewrite from one domain to a different domain with Exact Match

I want to set up URL rewrite on IIS so when
https://oldurl.abc.com/portal/apps/#/myapp
was hit, it will redirect to
https://newurl.abc.com/myapp
I have tried the following, but it didn't work. any thoughts?
Hash Tags in the URL serve a special purpose to the client browser, not to the server. That means that a browser will NOT actually send anything after a '#' character to the server. So: if you request http://someurl.com/index.aspx#something, the server only sees http://someurl.com/index.aspx.
You could try to use the below rule to resolve the issue:
<rule name="old to new redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^oldurl.abc.com$" />
<add input="{HTTPS}" pattern="on" />
<add input="{REQUEST_URI}" pattern="^/portal/apps/(.+)" />
</conditions>
<action type="Redirect" url="https://newurl.abc.com/myapp" />
</rule>

301 Redirect one domain to another using web.config

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.

Azure Cloud Service SSL redirect from Staging to Production slot

I have set up SSL on the production slot of my cloud service. In my Web.Release.config I have a rule to redirect from http to https. I deploy to staging with the Release configuration so that when I swap with production the redirect will take place. However, this causes my staging to redirect to the production environment, which means that I can never really test my staging deployment (basically I can test that the redirect is taking place).
I feel that this setup is wrong. Does anyone have any insight into whether my set up is incorrect?
EDIT: Web.Release.config rule:
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.myDomain\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.myDomaim.com/{R:1}" />
</rule>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
</system.webServer>
EDIT 2 I ended up going with #BenV 's approach of making sure that the redirect rule does not match my staging slot's URL. Makes sense :D
The issue is actually with your CanonicalHostNameRule1 rule. It basically says "redirect anything that is not www.mydomain.com to www.mydomain.com.
Since your staging slot is something like www.mydomain-staging.com, it gets redirected.
There's a few ways to go about fixing this, depending on what exactly you're trying to accomplish with that rule. One would be to add a rule to not redirect for staging.
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.myDomain\.com$" negate="true" />
<add input="{HTTP_HOST}" pattern="^www\.myDomain-staging\.com$" negate="true" />
</conditions>
This will do the redirect for anything that is not www.mydomain.com AND not www.mydomain-staging.com.
Another way to write this without referencing the staging slot would be:
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^myDomain\.com$" />
<add input="{HTTP_HOST}" pattern=".*myDomain\.azurewebsites\.net$" />
</conditions>
This checks if the request is for myDomain.com (without the "www") OR myDomain.azurewebsites.net (with or without the "www") and does the redirect if either of those is true. Since the staging URL would not match either of those it would be ignored.
(Disclaimer: I didn't test the regex's, but you get the idea)

Special non-www to www redirection under Amazon Elastic Load Balancer

How to create a 301 redirect in IIS7 using the Rewrite Rule extension for IIS (writes to web.config) that will allow redirecting non-www to www - BUT, I don't want to use (.*), I want the URL rewriter to ignore URLs like DNS ones, so it can work under Amazon Load Balancer. Without it, Amazon Load Balancer will not be able to read the health check file, because it will be redirected and instead of getting 200OK it will get 301.
So I actually need a rule that will match a URL that contains mydomain.com, or one that start with it of course, so it won't loop.
The current rule is not good:
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.mydomain\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:1}" />
</rule>
</rules>
</rewrite>
This will redirect URLs like dns.amzn.com to www.mydomain.com - I've tried it on IIS7.
Need a better solution
did you come with a solution..
i added a condition using {HTTP_USER_AGENT} to not match ELB (user agent).
This stopped the rule from running for ELB, but i'm just testing this at the moment.
cheers
Added This Also:
<rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_USER_AGENT}" pattern="ELB-HealthChecker" negate="true" />
<add input="{HTTP_X_FORWARDED_PROTO}" pattern="HTTPS" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
The bit you want is
<add input="{HTTP_USER_AGENT}" pattern="ELB-HealthChecker" negate="true" />