Http to Https for certain URLs using IIS - asp.net

ASP .Net website. Visual Studio 2013. Windows Server 2012 R2 + updates
I am attempting to redirect SOME pages as https i.e. login and payment pages AND pages under those directories. So far in IIS i have
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^.example\.com$" />
<add input="{HttpsRedirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{C:1}" appendQueryString="false" redirectType="SeeOther" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="HttpsRedirects">
<add key="/user/login/" value="/user/login/" />
<add key="/user/payment/" value="/user/payment/" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>
I have changed pattern="^www.example.com$" to pattern="^.example.com$" as the site's main domain name is example.com (without www).
The problem i have is it doesnt seem to add https to all pages below these directories i.e. all pages under the login and payment directories?
IF I manually add https then it also applies it to image files (im seeing images not looking correct).
How could i amend the above code to exclude images (file extension or directories) and ensure when the user reaches these pages they are using https?

Related

Azure redirect non-www to www

I have question about Azure redirection.
I have searched other topics but not quite my problem(s).
I have ASP.NET Core 2.2 project and Azure configured for that. Also i have domain for pointing to Azure.
I have added www.example.com format domain to azure and that works fine also with https.
Problem rises with example.com format
I wanted to redirect example.com format to www.example.com URL
I manage to get this work with this, but for little while.
I use this web.config for redirection
web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<rewrite>
<rules>
<rule name="CanonicalHostNameRule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Something strange is Google Chrome & Opera redirect(s) to www domain but not Edge and Firefox.
Then when i clear cache(s) then it wont redirect.
Seems like Azure like rewrites some over or something.
Thanks for help!!
In addition to the web.config file changes, ensure the Azure Website instance contains the correct domain bindings within the Hostname bindings area.
<rule name="Redirect to www">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^example.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent"/>
</rule>

Forcefully open site to www only .

I had hosted site in IIS which open in url like http://www.example.com as well as http://example.com
but i dont want to open it in http://example.com ,
http://example.com might change to http://www.example.com Forcefully, what confugeration shoud i wright in web.config
(Note: Updated answer below)
This requires you to make use of URL-rewriting.
This post here shows how to make use of the URL-Rewriting extensions:
http://www.dotnetexpertguide.com/2011/08/iis-7-redirect-domaincom-to.html
If you don't see this extension via IIS manager, you can download the extension here:
http://www.iis.net/download/URLRewrite
You will want to add a rule, look under "search engine optimization" and edit "canonical domain name".
There you can direct all traffic to the www-version of the domain by selecting it in the dropdown.
EDIT:
As for the question to do it in web.config, this is an example configuration:
<rewrite>
<rules>
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^domain.com$" />
</conditions>
<action type="Redirect"
url="{MapProtocol:{HTTPS}}://www.domain.com/{R:1}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>
Hope that helps,
Sebastian

URL Rewrite of domain name in IIS 7

I have two asp.net sites in IIS, api.mydomain.com and mobileapi.mydomain.com. and some of the requests to api should be rewritten to mobileapi. I cant get it to work. From my experiments it seems like I cant rewrite the domain name part of the url with rewrite. If I change to redirect everthing works (except that I want a rewrite and not redirect)
However, if I instead redirect to something on the same domain, lets say I swap the action url to /testing/{R:0} (without any domain name) then it works as expected and rewrites just fine.
My web.config
<modules runAllManagedModulesForAllRequests="true">
</modules>
<rewrite>
<rules>
<rule name="Proxy mobile API" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{REQUEST_URI}" pattern="([^/]+)" />
<add input="{StaticRewrites:{C:0}}" pattern="(.+)" />
<add input="{HTTP_HOST}" pattern="^api.mydomain.se$" />
</conditions>
<action type="Rewrite" url="http://mobileapi.mydomain.se/{R:0}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="StaticRewrites">
<add key="mobile" value=" " />
<add key="scripts" value=" " />
</rewriteMap>
</rewriteMaps>
</rewrite>
Depending on the project type you have setup, but if you are using MVC 4 you can simply make this switch in the Global.asax.cs file.
protected void Application_BeginRequest(){
//mobile device detection and redirection
if (Request.Browser["IsMobileDevice"] == "true"){
Response.Redirect("http://mobileapi.mydomain.se");
}
}

IIS Host Headers and non WWW to WWW

I know there's a bunch of examples on how to redirect your non www to your www site, but I'm not using any rewrite utils/ISAPI.
On my Windows 2008R2 box, I have several sites setup in IIS. I setup host headers for both www and non www versions. The first couple of sites work fine. If you try to go to the non www site, you are automatically redirected to the www version.
As far as I recall, I didn't have to do anything special other than add the appropriate host headers - no messing around with rewrites/ISAPI.
What am I missing on the server manager side of things in order to get this working?
I guess there are two ways. One is to create a rewrite rule through the IIS manager.
The other is to setup the system.webserver section of the web.config as follows:
<system.webServer>
<rewrite>
<rules>
<clear/>
<rule name="Redirect Non WWW to WWW" 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>
<!--<rule name="Default Document" stopProcessing="false">
<match url="(.*)default.aspx"/>
<action type="Redirect" url="{R:1}" redirectType="Permanent"/>
</rule>-->
</rules>
</rewrite>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
<httpErrors errorMode="Custom"/>
</system.webServer>

ssl and rewrite tag does not recognized by visual studio in mvc3 web site web.config

I've written a simple web application using asp.net mvc3 and I have a login page which control access to the site. Every request which is not authenticated will redirect to this page.
I want to use ssl to make the site secure (using https). I don't want to use https for all of my sites because it make it heavy for browsers.
The problem is when I add following tags to web.config, VS2010 ultimate does not recognize it and does not work.
I have deployed the project to IIS 7.5 too but it does not work either. what should I do?
<system.webServer>
<rewrite>
<rule name="Redirect to HTTP">
<match url="secureDir/(.*)" negate="true" />
<conditions>
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="secureDir/(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
</rewrite>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
You'll need the URL Rewrite extension for IIS and the rewrite tag only works with IIS, so you'll need to be using that for your project. You don't neccesarily need VS to validate your xml if it's correct, but if you want it, there's a guide here: http://ruslany.net/2010/04/visual-studio-xml-intellisense-for-url-rewrite-2-0/

Resources