Web.config Redirect to new site, with and without params - redirect

We have pulled out some code of our monolith, and build a separate application (With it's own url) instead. We need to be able to redirect links hitting the old monolith application new application.
Old url: https://OldApp.domain.com/OldAppContoller/123
New url: https://NewApp.domain.com/NewAppController/123
So when users go to the old url either with, or without the 123 id it should redirect to the new app.
Also if I'm running the Old application locally I need it to redirect direct it to our test environment "t-" like this:
Old url: https://localhost:7777/OldAppContoller/123
New url: https://t-NewApp.domain.com/NewAppController/123
And lastly I need to grab the environment prefix t,m,p to redirect to the correct environment.
I've been messing around with the web.config, but haven't found a working solution yet.
Here is what I've got:
<system.webServer>
<rewrite>
<rules>
<rule name="NewApp Localhost with Param" stopProcessing="true">
<match url=".*localhost.*OldAppContoller\/([0-9]*)"/>
<action type="Redirect" url="https://t-NewApp.domain.com/NewAppController/{r:1}" redirectType="Permanent" />
</rule>
<rule name="NewApp Localhost without Param" stopProcessing="true">
<match url=".*localhost.*OldAppContoller"/>
<action type="Redirect" url="https://t-NewApp.domain.com/" redirectType="Permanent" />
</rule>
<rule name="NewApp with Param" stopProcessing="true">
<match url=".*OldApp-([a-z]).*OldAppContoller\/([0-9]*)"/>
<action type="Redirect" url="https://{r:1}-NewApp.domain.com/NewAppController/{r:2}" redirectType="Permanent" />
</rule>
<rule name="NewApp without Param" stopProcessing="true">
<match url=".*OldApp-([a-z]).*OldAppContoller"/>
<action type="Redirect" url="https://{r:1}-NewApp.domain.com" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
I'm not sure if this is to be done with one rule, or multiple rules as my example above.
Any suggestions ?

Related

Redirect subdomain in IIS via web.config file

Looking to redirect old subdomain back to the primary domain name (remove the subdomain if it's seen) in the web.config file for IIS.
For example,
preview.mysite.com redirects to mysite.com
The rule I've tried is this:
<rule name="Redirect away from Preview" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="preview.mysite.com" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
It stops preview subdomain from loading, but shows error message instead of doing a redirect
This page isn’t working
preview.mysite.com redirected you too many times.
What can I do to correct the redirect rule to reroute any preview subdomain back to the base domain?
Got it fixed. Had to hardcode the redirect url and that fixed the looping issue, now redirects to the main domain:
<rule name="Redirect away from Preview" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="preview.mysite.com" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://example.com" redirectType="Permanent" appendQueryString="false" />
</rule>
Hope this helps someone else. (Note: stackoverflow would not allow me to put mysite for the url so it shows example.com instead)

IIS: Redirect multiple URL's to same website

I'm looking for the best way to redirect multiple URL's to one new URL.
I have the following URLS:
https://domain.app.company.com/application
https://application.app.company.com
And I want those to redirect to: https://application.domain.app.company.com
What would be the best way to go about this (in IIS)?
I have tried using URL Rewriting, but I can only make that work for the first URL (https://domain.app.company.com/application):
<rule name="Application 1" patternSyntax="Wildcard" stopProcessing="true">
<match url="*application" />
<action type="Redirect" url="https://application.domain.app.company.com" appendQueryString="false" />
</rule>
For the second URL (https://application.app.company.com), I can make it work when setting up a new (empty) website in IIS (that listens to said URL) and add a httpRedirect in it.
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="https://application.domain.app.company.com" exactDestination="true" httpResponseStatus="Permanent" />
</system.webServer>
</configuration>
Is this the recommended way to go about this? Or would there be another way?
Thank you
Schoof
If you want to bind two url for the same IIS web site, I suggest you could try to below url rewrite. We could create two url rewrite rule to achieve your requirement.
<rule name="Application 1" patternSyntax="Wildcard" stopProcessing="true">
<match url="*application" />
<action type="Redirect" url="https://application.domain.app.company.com" appendQueryString="false" />
</rule>
<rule name="test" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="application.app.company.com" />
</conditions>
<action type="Redirect" url="https://application.domain.app.company.com" />
</rule>

confusion regarding 301 redirect from folder to subfolder in web.config

Say I have my api under this url
http://www.example.net/api/trythis and I would like to redirect to http://www.example.net/api/v1/trythis, so in future I can have http://www.example.net/api/v2/trythis or http://www.example.net/api/v3/trythis
I am thinking of doing this in web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="Root Hit Redirect" stopProcessing="true">
<match url="/api/*" />
<action type="Redirect" url="/api/v1/*" />
</rule>
</rules>
</rewrite>
</system.webServer>
However, if someone access url /api/v2/trythis or /ap1/v3/trythis, won't they be redirected to /api/v1/trythis? So it will defeat the purpose of versioning?
According to your rules, it will cause multiple redirect errors. Since the "api/v1" also matches the "api".
To solve this issue, you should set the condition in your url rewrite rule to avoid multiple matching.
Details, you could refer to below rule:
<rule name="Root Hit Redirect" stopProcessing="true">
<match url="api/(.*)" />
<action type="Redirect" url="http://yousitedomain.com/api/v1/{R:1}" />
<conditions>
<add input="{PATH_INFO}" pattern="api/v1/(.*)" negate="true"/>
</conditions>
</rule>
If you want to avoid v2,v3, I suggest you could add multiple condition as above setting.

web.config rewrite complete URL

Hi there,
Just a simple question. I Have a Domain with path, let's say exampleme.com, and this page has a specific page: exampleme.com/game.php. Is there a way to completely rewrite the link to another domain on that page? So that exampleme.com/game.phpbecomes game.com?
I've tried this, but since I'm fairly new to the web.config rewrite module, I'm a bit clueless.
` <rule name="Examplerule">
<match url="http://game.com" ignoreCase="false" />
<action type="Rewrite" url="/game.php" appendQueryString="false" />
</rule>`
Rewrite works only on your current domain. You need an redirect.
<rules>
<rule name="Game" stopProcessing="true">
<match url="/game.php" />
<action type="Redirect" url="http://game.com" />
</rule>
</rules>

IIS rewrite rule to redirect specific domain url to different url on same domain

I simply want an IIS 7.5 rewrite rule to redirect http://www.domain.com/url1 to http://www.domain.com/url2 (same domain). This can be achieved by:
<rule name="Redirect url" enabled="true" stopProcessing="true">
<match url="^url1" />
<action type="Redirect" url="http://www.domain.com/url2"
appendQueryString="false" redirectType="Permanent" />
</rule>
However, this website listens to several domains, thus above becomes a global rule for all domains. How do I make this specific to domain.com? Have tried changing match url and adding conditions but cannot get it to work. Thanks.
I got it to work this way:
<rule name="Redirect url1" stopProcessing="true">
<match url="^url1$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?domain.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/url2"
appendQueryString="false" redirectType="Permanent" />
</rule>
Using the answer on this page, I was able to tweak a rule for myself. I also added query parameters. Wanted to post it here, in case it helps someone:
<!-- probably requires custom rewrite module, available through web platform installer -->
<rule name="Redirect oldsite.com" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^.*oldsite\.com$" />
</conditions>
<action type="Redirect" url="http://www.newsite.com/page.cfm?userid=123&mode=form"
appendQueryString="false" redirectType="Permanent" />
</rule>
Some bits of explanation:
To clear up some confusion, this "url" is the part after the first slash after the domain, and not the whole url. I'm going to put in this so that it gets any URL.
<match url=".*" />
Now, we'll add a condition because I had more than one web site on this computer, so I want to make sure this rule is applied to only one of them. I also used the wildcard instead of "(www.)?" because the wildcard will catch any subdomain.
<conditions>
<add input="{HTTP_HOST}" pattern="^.*oldsite\.com$" />
</conditions>
And the last note is for folks who want to put multiple query string params in. You'll need to escape the ampersand between them, because it won't work if you don't:
<action type="Redirect" url="http://www.newsite.com/page.cfm?userid=123&mode=form"
appendQueryString="false" redirectType="Permanent" />