IIS: Redirect multiple URL's to same website - redirect

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>

Related

IIS rewrite rule not passing parameters

I have redirects currently set up to go from oldsite.com to newsite.com using a rewrite map. They work except when a parameter is passed through the URL, ex. http://oldsite.com?utm_source=go.wayne.edu&utm_medium=direct&utm_campaign=quick-access&utm_content=, still goes to the old page instead of redirecting to the new page with the parameters, ex. http://newsite.com?utm_source=go.wayne.edu&utm_medium=direct&utm_campaign=quick-access&utm_content=.
Here's my code:
<rule name="Redirects to New Site">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{RelaunchRedirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="http://newsite.com/{C:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
Still very new to all of this and I can't figure out why the parameters are not passing. I've seen some similar cases on here but haven't been able to get those solutions to work for me.
Any ideas on what I can do to make this work?
You can try to use this URL Rewrite rule:
<rewrite>
<rules>
<rule name="Redirects to New Site" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
</conditions>
<action type="Redirect" url="http://newsite.com{URL}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Feel free to let me know if there are any questions.

www and http in kentico - IIS Redirect

I would like to create redirects to force https and for removal of www so all permutations direct to https://mysite.co.uk
This is a multi site Kentico system so I only want to redirect the specific domain in question.
Here's what I'm currently using but it's not working. Any help greatfuly appreciated.
<rewrite>
<rules>
<rule name="mysite https redirect">
<match url="^(mysite\.co.uk|www\.mysite\.co.uk)$" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
You can do this outside of IIS and leave it configured with the site only. If you look in Settings>URLS & SEO (after you select the site you want this configured for, upper left), you can make these changes there without a need to use IIS.
One thing to be sure of is to have your site's domain set as the primary domain you want to use. In your case, mysite.co.uk. Then in the site's domain aliases, add the entry www.mysite.co.uk.
Next, on the root of the site in the Pages app, go to Properties>Security and towards the bottom, select Requires SSL. This will replicate through all pages in the site.
I think you need 2 separate rules. Check this answer here:
<rule name="Remove WWW" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(ccsportal\.com)" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent"/>
</rule>

Rewrite subfolder only (HTTP to HTTPS) redirects to site root

I'm trying to rewrite HTTP to HTTPS for a specific subfolder on my IIS 8.5 web server but it's not working. I've read countless other solutions and blog postings but nothing I've tried works.
http://domain.example.com/one/two/three/
should redirect to... (same url but using https)
https://domain.example.com/one/two/three/
but instead is redirecting to... (site root using https)
https://domain.example.com
loading... (desired url with https)
https://domain.example.com/one/two/three/
also redirects to... (site root using https)
https://domain.example.com
it's removing the subfolders from the url.
This folder needs to also be protected with Windows Authentication, which I can get to work but the https redirection is failing with or without the authentication enabled so I don't think that's the cause.
Within IIS I selected the desired subfolder (/three/ in the example above) and created the Rewrite rule there.
<rewrite>
<rules>
<clear />
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
This should of course work with any files and folders contained within the desired subfolder. (/three)
I tried this and it redirects to the apparent correct url but gives the "too many redirects" error:
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="SeeOther" />
</rule>
You should avoid doing this:
Within IIS I selected the desired subfolder (/three/ in the example
above) and created the Rewrite rule there.
Instead setup the rewrite rules in Web.config at the application root. You can redirect a specific folder to HTTPS by including it in the match parameter as follows:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect Subfolder" stopProcessing="true">
<match url="^one/two/three/" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Please note that this is a minimal Web.config file that does what you are looking for. If your application already contains a Web.config in the root folder, then you will have to merge the above into your solution.

How to add RedirectType to External config file in asp.net

I have a separate .config file in the application root directory which contains Mapped URLS for redirect and referenced this .config file in web.config for 301 Permanent Redirect! This works fine.
See Reference Link
Now, i also want to add some links which will redirected as 302 status code. How to add 302 redirect in external .config file and redirect accordingly.
rewritemaps.config
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/oldcellphone" value="/newcellphones.aspx" />
</rewriteMap>
</rewriteMaps>
Can we specify the Redirect Type i.e 301/302 in this file?
web.config
<system.webServer>
<rewrite>
<rewriteMaps configSource="rewritemaps.config">
</rewriteMaps>
<rules>
<rule name="Redirect rule1 for Redirects">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
NOTE:Currently all links from file 'rewritemaps.config' are set to 301 Status in web.config.
Can we add as following in rewritemaps.config and redirect accordingly:
<add key="/oldcellphone" value="/newcellphones.aspx" [RedirectType=301] />
<add key="/oldphone" value="/newphones.aspx" [RedirectType=302] />
There are about 1000 links of 301 Status and about 400 links for 302 Status. If its not possible in external file(rewritemaps.config) then please suggest preferred way to do?
Update:
Can you help me to redirect to another site(different domain) if specific string match in requested URL .
Eg: if the requested URL contains "/hm1" then redirect to different site. i.e http://www.google.com
Web.config
<rule name="othersite" stopProcessing="true">
<match url="^/hm1$" />
<action type="Redirect" url="http://www.google.com" redirectType="Found"/>
</rule>
.aspx
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="/hm1">other site (http://www.google.com)</asp:HyperLink>
Can you add RedirectType into a Rewrite Map? No, unfortunately not.
To achieve what you're trying to do you're going to need to create two Rewrite Maps and two Rewrite Rules - one for 301 redirects and one for 302 redirects.
Here's an example of how that might look:
<rewrite>
<rules>
<rule name="301Redirects" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
<conditions>
<add input="{301Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
</rule>
<rule name="302Redirects" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Found" />
<conditions>
<add input="{302Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="301Redirects">
<add key="/oldurl" value="/newurl" />
</rewriteMap>
<rewriteMap name="302Redirects">
<add key="/oldcellphone" value="/newcellphones.aspx" />
</rewriteMap>
</rewriteMaps>
</rewrite>

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" />