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

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

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.

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>

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>

Trouble getting redirects right using web.config for IIS

Ok have a strange redirect issue here. I have 3 domain names so lets call them the following
maindomain.com
aliasdomain.net
aliasdomain.org
We are using Let's Encrypt for https via IIS on a windows 2016 Server.
What we want is for anytime a person types in any of the 3 domains with or the www to all redirect to the domain name www.maindomain.com
Since the let's encrypt certificate is not creating www. versions for the domain aliases it is causing us some struggles. This is the web.config rules we are using but they do not work.
<rule name="Redirect to WWW" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\." negate="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{HTTP_URL}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="Redirect to HTTPS" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{HTTP_URL}" redirectType="Permanent" appendQueryString="false" />
</rule>
So the results of this are the following
This works
www.maindomain.com works and redirects to https://www.maindomain.com
maindomain.com works and redirects to https://www.maindomain.com
This doesn't work and oddly enough shows the standard IIS landing page
aliasdomain.net doesn't work redirects to http://www.aliasdomain.net
www.aliasdomain.net doesn't work and redirects to http://www.aliasdomain.net
This one doesn't work at all
aliasdomain.org doesn't work redirects to https://www.aliasdomain.org
www.aliasdomain.org doesn't work and redirects to https://www.aliasdomain.org
No idea how to get this to do what we want. You would think it would not be some difficult to redirect any version of any domain name to the https://www.maindomain.com
Thanks for any help here.
The problem with your rule is that you are using {HTTP_HOST} in your
redirect action.This parameter will take the incoming hostname from
the request.I think that is not what you want. <action
type="Redirect" url="https://{HTTP_HOST}{HTTP_URL}"
redirectType="Permanent" appendQueryString="false" />
Please try below rule. It redirects if the hostname does not match
www.maindomain.com ,also enforce https
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^www\.maindomain\.com$" negate="true" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.maindomain.com/{R:1}" />
</rule>

IIS 7 rewrite rule - redirect based on presence of querystring

Is it possible to redirect using web.config based on the presence of a querystring in the initially requested URL? I'm not sure what to include in the the conditions. I'm a beginner at working with rewrite/redirect rules in web.config and would like to learn more about syntax, parameters etc.
I'm trying to do something like this:
<rewrite>
<rules>
<rule name="Restricted Folder with Querystring" stopProcessing="true">
<match url="^test folder/(.*)" ignoreCase="false" />
<conditions>
<!--redirect to a certain page if a certain querystring is present -->
</conditions>
<action type="Redirect" redirectType="Permanent" url="/test folder/{R:1}" />
</rule>
<rule name="Restricted Folder without Querystring" stopProcessing="true">
<match url="^test folder/(.*)" ignoreCase="false" />
<conditions>
<!--redirect to another page if querystring is not present -->
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://www.whatever.com/page.asp?url={R:1}" />
</rule>
</rules>
</rewrite>
deeholzman, I believe for your Condition - Add Input example you want to use "pattern" and not "matchType". EX:
<add input="{QUERY_STRING}" pattern="^whateverpattern" />
or
<add input="{QUERY_STRING}" pattern="^whateverpattern" negate="true" />
I found the answer in the URL Rewrite Module Configuration Reference. In the rewrite section of web.config in whatever folder you wanted to perform the redirect from, it would go something like this:
<rules>
<rule name="Restricted Folder Gimbal" stopProcessing="false">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="^whateverpattern" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.whatever.com/file.html" />
</rule>
</rules>
For a querystring not present, you could add the 'negate' parameter to the condition:
<add input="{QUERY_STRING}" pattern="^whateverpattern" negate="true" />
The the URL Rewrite Module Configuration Reference is a very handy reference that unfortunately took me longer than expected to find.