Add a hostname to a specific folder within Azure as another domain - azure

So I have a project in azure that is www.example.com and it has it's MVC file structure with source code and all. However, within that, I have another part that needs it own URL, www.example2.com which would point to a folder within this hierarchy. So you could reach this entry point from www.example.com/#/example2.html or simply from going to www.example.com. Hope that makes sense. So within Azure, how can I achieve this? I am looking through custom domain, but not seeing a way to go about this. Any ideas please?

Yes, using rewrite rules in your web.config. Adjust variables accordingly.
<rewrite>
<rules>
<rule name="Redirect domain" enabled="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?example2\.com$" />
</conditions>
<action type="Rewrite" url="folder/{R:1}" />
</rule>
</rules>
</rewrite>
Don't forget to add and authorize both domains to your web app.

Related

Url rewriting/routing on .Net Core with IIS and Cloudflare

A small introduction about the app's deployment & app in general:
We use .Net Core 6 MVC and is hosted in IIS. Additionally the application uses a domain which the actual website is hosted e.g. www.ExampleHost.com/ExampleApp.
Also we use Cloudflare to direct requests to a new domain for the website e.g. www.ExampleHost.com/ExampleApp -> www.Example-App.eu
Here is the problem, while the app works fine at www.ExampleHost.com/ExampleApp, if we enter www.Example-App.eu the directory of the app appears in the Url like www.Example-App.eu/ExampleApp in every request. That creates problem everywhere from returning Views to submitting Forms etc.
So an idea was to solve part of this problem by creating a web.config to configure URL Rewrite rules.
web.config
<rewrite>
<rules>
<rule name="RemoveExampleApp">
<match url=".*ExampleApp/(.*)" />
<conditions>
<add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}"/>
</rule>
<rule name="RemoveExampleAppPost">
<match url=".*ExampleApp/(.*)" />
<conditions>
<add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="{R:1}" />
</rule>
</rules>
</rewrite>
But we still unable to perform POST requests.
Additionally, I read to another Stack Overflow post that it is possible to configure/fix that by of using routing instead of rewrite rules but we had no luck with that.
IIS URL Rewrite and .NET Core
We are open to any suggestions.
Thanks for your time.

IIS URL rewriting has trouble with subcalls

I have two virtual machines. The first one (VM1) is running a web application with an URL like this:
VM1/servicedesk/customer/user/login
The second (VM2) should now redirect to this address without changing the URL and also doesn't allow to redirect to the root / since that is a different web-application. This works relatively easily with this rewrite rule:
<rule name="rewriteAll" stopProcessing="true">
<match url=".*" />
<action type="Rewrite" url="http://VM1/servicedesk/customer/user/login" />
</rule>
It just basically rewrites everything from VM2 to the specific VM1 URL.
The problem I'm facing is that this web-application has many Ajax calls to other addresses on the same VM1.
For example VM1/rest/... or VM1/s/.... I really tried to find each exceptional call and create a rule before this default Rewrite. But since some of them are nested and could be changed this isn't a good approach. So what i need is basically a rewrite without changing the URL what doesn't break the application that does a lot of nested calls.
I found out that i can use the following rule (redirectRest) before the existing rewriteAll rule to redirect subcalls (VM1/rest/..).
<rule name="redirectRest" stopProcessing="true">
<match url="/.*" />
<action type="Redirect" url="http://VM1/{R:0}" />
</rule>
<rule name="rewriteAll" stopProcessing="true">
<match url=".*" />
<action type="Rewrite" url="http://VM1/servicedesk/customer/user/login" />
</rule>
So basically when I call VM1 the second rule applies and I get rewritten to the web-app. The application hosted there will call e.g. VM1/rest what triggers the first rule and redirect the Ajax calls to the root. But I've found out that Ajax calls are not following a redirect like 301.

IIS URLrewrite: How to preserve original host name for internal redirects?

I have a web application running on one server and use IIS with rewriteURL on another server as a reverse proxy.
I configured the incoming rules to rewrite the reverse proxy address with the address of the web application server. This works fine.
However, the application allows users to download some content and redirects them to the download address. Right now, users get forwarded to the local IP address of the web application server, not the public address of the reverse proxy.
I understand, I need to edit the outbounding rules as well to catch this, but I somehow don't get it right.
I followed the instructions here
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/modifying-http-response-headers
and my current rewrite rules look like this
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="er-platform" stopProcessing="true">
<match url="^er-platform(.*)" />
<conditions>
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<action type="Rewrite" url="{C:1}://192.168.80.6:8443/{R:0}" />
<serverVariables>
<set name="ORIGINAL_HOST" value="{HTTP_HOST}" />
</serverVariables>
</rule>
</rules>
<outboundRules>
<rule name="er-platform" preCondition="IsRedirection" enabled="true">
<match serverVariable="RESPONSE_LOCATION" pattern="^(https?)://[^/]+/(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{ORIGINAL_HOST}" pattern=".+" />
</conditions>
<action type="Rewrite" value="{R:1}://{ORIGINAL_HOST}/{R:2}" />
</rule>
<preConditions>
<preCondition name="IsRedirection">
<add input="{RESPONSE_STATUS}" pattern="3\d\d" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
I still get redirected to the local IP when trying to download something.
How do I need to change the rules to get this right?
Thanks,
Thomas
I don't think your outbound rule is incorrect. I have a couple of guesses for why it is failing.
First: Your inbound rule must match for your outbound rule to work. This is because ORIGINAL_HOST is captured when the inbound rule executes. Right now, your inbound rule is match the URL against ^er-platform(.*). I am going to assume that since this is a reverse proxy, that it is working because you wouldn't be able to start the download if it wasn't.
Second: The output rule only triggers on a 3xx code. This isn't the only way to redirect though. It's possible that you're redirecting using JavaScript. E.g. `window.location = 'http://wrongaddress'. In which case your outbound rule wouldn't work.
Next steps for debugging this:
Turn on Logging for the inbound rule.
Your logs will be written to %SystemDrive%\inetpub\logs\LogFiles\. Verify the inbound rule is being hit.
Verify what's happening on the wire:
Fiddler is a great tool for understanding what's actually happening on the wire. Use this to confirm that the URL rewrite rules should be hitting. I.e. that the request URL matches ^er-platform(.*) and that the response code is in the 300s.
Alternatively to Fiddler you could just get away with using Chrome Developer Tools Network tab. Turn on the 'Preserve log' checkbox so that it doesn't clear after you've been redirected. Verify what you think should be happening is actually happening.

Problem with URL rewrite on IIS 7.5

I'm trying to use the URL Rewrite module for IIS 7.5 to redirect all HTTP requests to HTTPS for my ASP.NET website. The site works fine at the moment but forces the user to type the https:// in the address bar.
I followed the instructions in this article. Everything seems to be fine: I've tried putting the rule in the web.config and it shows up in the UI as it should; I've also done the reverse and can see the changes in the web.config when I use the UI to add the rule. I have RequireSSL unchecked for the site. Unfortunately I still just get a 404 when I try to hit the site via http://.
I've tried a few different action urls including {HTTP_HOST}/{R:1} and the one shown below.. nothing works.
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"
redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
</system.webServer>
I'm fairly new to this and pretty frustrated at this point. Seems like this should be a lot easier. Any advice would be appreciated, thanks..
Reposting from ServerFault as it's been sitting unanswered for a while.
HTTP Error 404. The requested resource is not found
Do you actually have binding for HTTP 80 port? Sounds like you do not have it (only HTTPS).
The reason I'm asking is the quoted text is the exact message that I would see if I request unknown to IIS domain (when there is no catch-all defined) or domain is not bound to the requested port.

Redirect visitors to the canonical URL for a page using IIS

I want to ensure that anybody who goes to http://example.com/* gets automatically redirected to http://www.example.com/*. Currently, IIS allows either URL form to work, meaning that any page can be accessed at multiple URLs, which has a number of disadvantages (SEO, etc).
Is there any way to do this built into IIS (especially IIS 6) without setting up a third-party rewriting engine like this? It seems like a bazooka to kill a mosquito.
The easy way would be to simply remove the DNS entries for 'www.mysite.com', so the only DNS entries that exist are for 'mysite.com'.
Alternatively, here's a couple of techiques for redirecting to a canonical URI:
http://www.kalyani.com/2010/01/redirecting-to-canonical-url-in-iis7/
https://web.archive.org/web/20211020203216/https://www.4guysfromrolla.com/articles/072810-1.aspx
http://www.stevenhargrove.com/redirect-web-pages/
Basically you want to hand back a 301 Moved Permanently status for the non-canonical URIs, along with the canonical URI so the user agent may load it instead.
I have another solution for you:
<rule name="Canonical domain name" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>

Resources