web.config redirect in IIS server - iis

I have to redirect from a domain to another.
I try different ways to do this but it doesn't work.
Someone can tell me the right metod to do a redirect in web.config?

Without knowing the ways you tried, I would suggest setting up a Global URL Rewrite rule, you do this at the server level not the individual web site level.
These setting will be stored in the applicationhost.config (Server level), not the (web.config)
Below will redirect example2.com to example.com
<rule name="Redirect to new domain" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example2\.com$" />
</conditions>
<action type="Redirect" url="https://www.eample.com/{R:0}" />
</rule>

Related

IIS Rewrite rule for non-www to www not working

We have a bunch of websites hosted in IIS using Flex. There are quite a lot of Rewrite/Redirect rules for each of them. One of them now needs a Rewrite rule to direct a non-www url to a www url. Have already tried quite a few popular solutions from various sites including other SO question. The last I tried is as following, without much to avail:
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^your.domain.name$" />
</conditions>
<action type="Redirect" url="http://www.your.domain.name/{R:0}" redirectType="Permanent" />
</rule>
Ensure you have the latest URL Rewrite extension.
https://www.iis.net/downloads/microsoft/url-rewrite
Before testing the result, please clean the local cache. We had better verify it in an inprivate window.
Besides, Failed Request Tracing is a powerful tool for troubleshooting request-processing failures. FRT can be used with the URL rewrite module to trace how rewrite rules were applied to the request URL.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules
At last, please try the below configuration.
<rule name="Force www" enabled="true" stopProcessing="false">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com{REQUEST_URI}" />
</rule>
Feel free to let me know if the problem still exists.

Second domain pointing to subfolder of first domain won't rewrite port in IIS 8 URL Rewrite

To try and make this as short as possible, I have two domains at work.
The first domain is our main domain at www.site1.com (port 80), pointing to a regular set up of a site in IIS 8, Server 2012.
The second domain is at www.site2.com (port 8085), but using some clever URL rewrite wizardry that I found on SO, www.site2.com points to www.site1.com/site2/, but behaves as if it were it's own domain. We did this in order to share assets between site1 and site2 in a .NET environment.
I do not have DNS-level control, but have admin of the server and it's settings and have done all of the URL rewriting for both sites myself. We received a request to install an SSL cert on www.site2.com (port 444) so that we can set the site to HTTPS. This is not relevant to the problem in itself because the problem exists in HTTP already.
The problem came about because another developer wants to be able to test the cert and the port configurations. As of right now, this isn't possible even in HTTP. Typing in www.site1.com:80 on the server works fine, but www.site2.com:8085 does not. site2 only connects properly when appended with www.site2.com:80 or just by typing in www.site2.com, I believe since it is rewritten as a subfolder off of site1.
I have tried everything I could think of in order to try and rewrite the ports in the URL so that the developer is able to test the port configuration by typing in www.site2.com:8085. The URL never resolves.
The bindings are set correctly as well because www.site2.com works fine in both test and production and has for years. It only does not work when the correct port is added to the URL.
This is the only code triggering the rewrite as it stands:
<rule name="www.site2 redirect" enabled="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www.)?site2.com$" />
</conditions>
<action type="Rewrite" url="/site2/{R:0}" />
</rule>
Any and all help would be appreciated. Thanks.
You can do that in this way, just add one more condition into your rewrite rule:
<rule name="www.site2 redirect" enabled="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www.)?site2.com$" />
<add input="{HTTP_HOST}" pattern="^(www.)?site2.com:8085$" />
</conditions>
<action type="Rewrite" url="/site2/{R:0}" />
</rule>
Another way to to that with one condition is (remove $ from pattern in condiiton):
<rule name="www.site2 redirect" enabled="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www.)?site2.com" />
</conditions>
<action type="Rewrite" url="/site2/{R:0}" />
</rule>
But this rule will capture domains like that as well: site2.com.au, site2.com.fr, site2.com:{ANYPORT} etc. That's why i prefer first rule

Domain Redirect from domain.com/pathA to domainA.com

I have an IIS web server that is setup to handle all requests to *.myapp.com. We also have clients that have setup a custom domain, e.g. custom.customer.com as a CNAME To customer.myapp.com
I have a requirement to handle the following:
Requests to custom.customer.com/pathA will need to be redirected to a.internal-app.com
Requests to custom.customer.com/pathB will need to be redirected to b.internal-app.com
The problem I have right now is that this can't be done on the DNS level (as it involves paths). Also, whatever the "redirect" is I want the client to see his custom domain at all times. Like he should never see internal-app.com or myapp.com.
Is this possible in any way?
It is possible with reverse proxy.
1) You need to install URL Rewrite and ARR module for IIS
2) Enable ARR. On the Application Request Routing page, select Enable proxy
3) Create rewrite rule
<rewrite>
<rules>
<rule name="rewrite custom.customer.com/patha" stopProcessing="true">
<match url="patha" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^custom.customer.com$" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="http://a.internal-app.com/" />
</rule>
<rule name="rewrite custom.customer.com/pathB" stopProcessing="true">
<match url="pathb" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^custom.customer.com$" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="http://b.internal-app.com/" />
</rule>
</rules>
</rewrite>
P.S. You might have problem with your resources(images,styles,css,js). Because your html might contains absolute paths to resources
You can check this post, when author is creating outbound rule for fixing relative urls https://blogs.iis.net/carlosag/setting-up-a-reverse-proxy-using-iis-url-rewrite-and-arr

Redirect works but rewrite doesn't on an Azure web app

I am trying to redirect from my old ISP to my azure web site. I have set up the necessary DNS records. As I am using sub-folders on my Web App I need to set up redirection rules in the root web.config file to point from the domain name to the correct sub-folder of the main site.
It works perfectly when I have a redirect rule.
<rule name="Works" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite.com$" />
</conditions>
<action type="Redirect" url="http://mysite.azurewebsites.net/mysub"/>
</rule>
However when I change to a Rewrite it fails. I really want a rewrite as I don't want the user to see the change of url in the browser.
<rule name="Fails" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite.com$" />
</conditions>
<action type="Rewrite" url="http://mysite.azurewebsites.net/mysub"/>
</rule>
I get the following message:
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
What am I doing wrong?
Your rewrite action should be a relative path, see the documentation:
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Rewrite_action
A substitution string must always specify the URL path

IIS URL Rewrite for redirect to FQDN

I am trying to figure out the best URL rewrite rule to accomplish the following.
http://intranet/sites/default.aspx rewrite to http://intranet.domain.com/sites/default.aspx
http://intranet rewrite to http://intranet.domain.com
Also in IIS the URL binding is set to "intranet" for that web application
Hope that makes sense. Can someone please help with the rewrite rule?
This is the rule I would use:
<rule name="Intranet redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^intranet$" />
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="http://intranet.domain.com/{R:0}" />
</rule>
It will match any requested path (url="(.*)") on the host exactly named http://intranet (pattern="^intranet$" and with https been turned off) and redirect it to http://intranet.domain.com/{R:0} (where {R:0} is a back reference containing whatever path was requested).

Resources