URL rewrite to a different virtual directory without changing URL - iis

I have a multi tenant web application that uses one virtual directory in IIS.
Example:
foo.example.com/100
boo.example.com/100
I want one of my clients to view some new features but dont want to change the URL. I created another virtual directory called preview. How do I configure URL rewrite to point foo.example.com/100 to the new virtual directory "preview" without changing the URL (foo.example.com/100)? Thanks!
<rule name="Preview URL Rewrite" enabled="true" stopProcessing="true">
<match url="100/(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^foo.example.com$" />
</conditions>
<action type="Rewrite" url="preview/{R:1}" appendQueryString="true" />
</rule>

Related

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>

Do I need anything besides code written in web.config to redirect website hosted on azure to https

I have a website that I host on azure. I recently bought an SSL and configured it. Now users can visit my site by typing in either http://example.com or https://example.com.
What I want is for users who type in the former to be automatically redirected to the latter while also keeping anything after the .com
So if a user types in http://example.com/about they will be redirected instead to https://example.com/about.
After some reading, I've come across this code that seems to do what I want
<system.webServer>
<rewrite>
<rules>
<rule name=”Redirect to https”>
<match url=”(.*)”/>
<conditions>
<add input=”{HTTPS}” pattern=”Off”/>
<add input=”{REQUEST_METHOD}” pattern=”^get$|^head$” />
</conditions>
<action type=”Redirect” url=”https://{HTTP_HOST}/{R:1}”/>
</rule>
</rules>
</rewrite>
</system.webServer>
But before I add this to my web.config file I have a few questions.
What is the IIS url rewrite module? IIS Rewrite and is it required to be installed on my azure hosted websites before I upload my new web.config file.
How can I also include removing www from my URL when a user enters it. For example if a user types in www.example.com they should be redirected to https://example.com instead. The reason that I want this is because in my google search console I've told google to display URLs as example.com rather then www.example.com
and finally, will this code do what I'm looking for? Is there a more professional way to achieve this? What are the benefits. I should note that my sites are asp .net web forms. I know MVC has routing options but that is not an option for me.
Edit : I don't think How to force HTTPS using a web.config file solves my issue because I don't even know if I can install the URL Rewrite module since I am not hosting IIS myself. Does azure give you access to the IIS settings? I am unfamiliar with azure details.
The Microsoft URL Rewrite Module for IIS enables IIS administrators to create powerful customized rules to map request URLs to friendly URLs that are easier for users to remember and easier for search engines to find.
This module is pre-installed for Azure Web App, as shown when inspect the applicationHost.config of the Azure Web App in Kudu.
Hence, you do not need to worry about the availability of the module for Azure Web App.
The URL Rewrite configuration to enforce HTTPS redirection for Azure web app is the simplest way to achieve what you intend. Your above configuration will apply
only if the request method is either HTTP GET or HTTP HEAD. The below configuration will not have such limitation.
<system.webServer>
<rewrite>
<rules>
<rule name="Force HTTPS Redirection" enabled="true" stopProcessing="true">
<match url="^$" ignoreCase="false"/>
<conditions>
<add input="{HTTPS}" pattern="^OFF$"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
I would add one last thing. Assuming you are running on Azure Web Apps, they have various probes to your site for warm up and initialization. You probably don't want these probes to also be redirected, otherwise, you may have some issues when you restart or use Azure's swaps feature for stuff like blue/green deployments. These probes would then be return with a 301/302 rather than actually hitting your site (and Azure doesn't actually follow the redirect)
More examples https://github.com/projectkudu/kudu/wiki/Xdt-transform-samples
<rule name="Redirect to non-WWW" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com$" />
<add input="{HTTP_USER_AGENT}" pattern="Initialization" negate="true" /> <!-- IIS Application Initialization Warmup -->
<add input="{HTTP_USER_AGENT}" pattern="SiteWarmup" negate="true" /> <!-- Azure WebApps Warmup Request -->
<add input="{HTTP_USER_AGENT}" pattern="AlwaysOn" negate="true" /> <!-- Azure WebApps AlwaysOn Probes -->
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://example.com/{R:1}" />
</rule>
<!-- Redirect to HTTPS Version -->
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_USER_AGENT}" pattern="Initialization" negate="true" />
<add input="{HTTP_USER_AGENT}" pattern="SiteWarmup" negate="true" />
<add input="{HTTP_USER_AGENT}" pattern="AlwaysOn" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>

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 7 URL Rewrite difficulties

To configure an IIS rewrite rule to redirect traffic to my site to a different URL.
Basically I am trying to redirect this url: www.sharepointalex.co.uk to www.sharepointalex.co.uk/blog. At the moment it is pointing at the root of my site (a wedding website I setup for my partner and I).
www.sharepointalex.co.uk is a domain pointer on WinHost.
The rule I have so far is:
<rules>
<rule name="Blog" patternSyntax="ExactMatch" stopProcessing="true">
<match url="www.sharepointalex.co.uk" />
<action type="Redirect" url="{R:0}/blog" redirectType="Temporary" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.sharepointalex.co.uk" />
</conditions>
</rule>
</rules>
However this doesnt seem to work.
Any help would be greatly appreciated.
Cheers
The match URL should not include the host name.

IIS 7 Canonical URL redirect

I would like to make a website always have www in the address, and enforce it via IIS rewrite.
For example, test.com would become www.test.com.
The typical example rewrite rule is:
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.test\.com$" />
</conditions>
<action type="Redirect" url="http://www.test.com/{R:1}" redirectType="Permanent" />
</rule>
However this requires me to enter the full url of my website. It will not work for development and staging environments that have URLs like www.test.dev and www.test.stage.
Is it possible to create an IIS Rewrite rule that will handle all of those cases?
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>
You're right that the full URL needs to be in the web.config. You have options though.
You can use a config transform to make the regular expression match the correct environment.
There doesn't seem to be any harm if you include all three URL rewrite rules in your web.config. It sounds like your environments are isolated so each environment would only ever match one of the rules. That can clutter your web.config, but not horribly.
I'd go with option 1. You can find information on config transforms here: http://msdn.microsoft.com/en-us/library/dd465326.aspx
I would also suggest you another variant for testing in local environment:
Add to c:\Windows\System32\drivers\etc\hosts:
127.0.0.1 www.example.com
In IIS Manager select site and right click -> Edit bindings.. -> Add..
Enter host name: www.example.com
Open cmd and run iisreset
Now you are able to use www.example.com in browser which is mapped to localhost

Resources