IIS redirect to full domain name - iis

I need to redirect from
http://someserver/someapplication.page.aspx
to
http://someserver.domain.com/someapplication.page.aspx
Both the requests lead to the same server.
someserver/ works through our company's internal DNS
This is the same question as Redirecting to Full Domain
but I want an IIS solution for this, not code. My guess is it will have something to do with adding a httpRedirect add element in Configuration Editor using wildcards.

You can use URL Rewrite for that which is the recommended way to do it in IIS, simply add a web.config with a rule like:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to full domain" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^someserver$" />
</conditions>
<action type="Redirect" url="http://someserver.domain.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Related

URL Rewrite Rules for domain and subdirectory

We're trying to rewrite to a specific domain and subdirectory, when we rewrite to the domain it works fine but trying to get to the subdirectory either brings up the wrong page or doesn't work at all.
Here is what we have now that is working to rewrite the domain, but doesn't go to the proper subdirectory and page.
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to site.firstdomain.com">
<match url="(.*)" ignoreCase="true" />
<action type="Rewrite" url="https://site.firstdomain.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
With the above set up entering https://site.seconddomain.com takes us to https://site.seconddomain.com/Account/Login?ReturnUrl=%2f
But we really want to go to
https://site.seconddomain.com/Account/Login?co=7xTixDp4F%2fzAp0WobaTTjw%3d%3d
But if I try
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to site.firstdomain.com">
<match url="(.*)" ignoreCase="true" />
<action type="Rewrite" url="https://site.firstdomain.com/Account/Login?co=7xTixDp4F%2fzAp0WobaTTjw%3d%3d" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
We still don't get taken to the https://site.seconddomain.com/Account/Login?co=7xTixDp4F%2FzAp0WobaTTjw%3D%3D address and the page displays the wrong images and then eventually Chrome says there is a memory error.
Is this something to do with the special characters in co=7xTixDp4F%2FzAp0WobaTTjw%3D%3D ?
Or I'm beginning to think this isn't a URL rewrite issue and is something with the page/system, any ideas?

IIS and Azure Web App not picking URL Rewrite rule

I want to redirect a particular url to another server. Following is my rewrite rule:
<rewrite>
<rules>
<rule name="mydomain" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(http|https)(://domain1\.)(example\.)(com|net)(/research)(.*)" />
<action type="Redirect" url="{R1}://{R3}{R4}/domain1research" />
</rule>
</rules>
</rewrite>
I am expecting that if a user types https://domain1.example.com/research, they should be redirected to https://example.com/domain1research. I've tested the expression with multiple variations on the url (.com, .net and query string) and it matches always. But when I run the website it never gets re-directed to the other page. I've URL rewrites in many web apps and they all work fine. I am not able to put my fingre on what I'm missing here.
I have tried it locally with an entry in hosts file and in a published website (Asp.Net MVC) on Azure but nothing works.
As per #lex Li's blog post and Microsoft documents, following rewrite works:
<rewrite>
<rules>
<rule name="mydomain" patternSyntax="ECMAScript" stopProcessing="true">
<match url="research" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="(domain1\.)(example\.(com|net)" />
</conditions>
<action type="Redirect" url="https://{C:2}/domain1research" />
</rule>
</rules>
</rewrite>

Rewrite URL for path to domain in Azure App Service using Virtual Directory

I have set up an Azure App Service to use Virtual Directories.
The path my-app.azurewebsites.net/api is working correctly with the site wwwroot/api.
I have a CNAME record for api.mydomain.com to my-app.azurewebsites.net which works just fine as well. I can successfully call api.mydomain.com/api.
However I'd like to use a subdomain instead of the path, rewriting api.mydomain.com to api.mydomain.com/api
I've added a web.config file in my wwwroot directory which looks like following now
wwwroot/
- api/
- web.config
web.config:
<rewrite>
<rules>
<rule name="My redirection">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^api.mydomain.com$" />
</conditions>
<action type="Redirect" url="https://api.mydomain.com/api/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
After a restart, the App Service is not responsive and does not serve requests anymore. Is there a better way to achieve this or do I need to modify my rewrite?
After a few attempts I figured the mistake. Redirect has to be changed into Rewrite.
<rewrite>
<rules>
<rule name="My redirection">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="api.mydomain.com$" />
</conditions>
<action type="Rewrite" url="api/{R:1}" />
</rule>
</rules>
</rewrite>

Redirect to canonical URL is not working

I'm trying to set the canonical URL for my site (macton.com) to add the www in the beginning. The site is hosted using IIS and I installed the URL Rewriter Extension.
Here is the code I put in the web.config file. However, it doesn't seem to do anything, because it remains macton.com.
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to www" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^macton\.com$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.macton.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
After this I have a bunch of <location> tags with redirects that work fine. I tried using a redirect from macton.com → www.macton.com but that just creates an infinite redirect loop (DUH!)
Any idea why this wouldn't be working? Everywhere I look says this is the correct code!
This link can help:
http://www.awseibert.net/how-to/redirecting-canonical-names-in-iis-7
Have you also added macton.com to the binding of your website?

IIS7.5 URL Rewrite rule to perform a 301 redirect from mysite.hosting.com to mysite.co.uk

I use IIS 7.5
I have a website wich has a valid host like:
A) mysite.co.uk
and a DEFAULT host (using for testing proposes provided by the hosting company):
B) mysite.hosting.com
Website is visible on both address, creating a DUPLICATE CONTENT issue for Search Engine.
I need redirect all the traffic (for all pages) from B to A using a 301 redirect.
IIS7.5 Http Redirect it is not design for this situation so I suppose to use IIS 7.5 Url Rewrite Module.
My questions: how write the ROLE in my web.config? Thanks
Try adding something like this between the <System.webServer> tags in your web.config:
<rewrite>
<rules>
<rule name="Redirect mysite.hosting.com to mysite.co.uk" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="mysite.hosting.com" />
</conditions>
<action type="Redirect" url="http://mysite.co.uk/{R:0}" />
</rule>
</rules>
</rewrite>
Alternatively, you can do this using global rules by adding:
<rewrite>
<globalRules>
<rule name="Redirects to mysite.co.uk" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="mysite.hosting.com$" />
</conditions>
<action type="Redirect" url="http://mysite.co.uk/{R:0}" />
</rule>
</globalRules>
</rewrite>

Resources