URL Rewrite of domain name in IIS 7 - asp.net

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");
}
}

Related

Http to Https for certain URLs using IIS

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?

Redirect in IIS 7.5/8 with regular expression

I need to redirect a lot of URLs like the following
http://localhost/OmniService/foto/18443151-810079.jpg
so, in my web.config I set the following regular expression:
<rewrite>
<providers>
<provider name="DB" type="DbProvider, Microsoft.Web.Iis.Rewrite.Providers, Version=7.1.761.0, Culture=neutral, PublicKeyToken=0545b0627da60a5f">
<settings>
<add key="ConnectionString" value="Driver={SQL Server Native Client 10.0};Data Source=localhost\sqlexpress;Initial Catalog=RewriteDB;Integrated Security=True;Server=localhost;uid=sa;pwd=working2014" />
<add key="StoredProcedure" value="RewriteDB.dbo.GetRewrittenUrl" />
<add key="CacheMinutesInterval" value="0" />
</settings>
</provider>
</providers>
<rules>
<rule name="DbProviderTest" stopProcessing="true">
<match url="(OmniService/foto/([0-9]+)-([0-9]+).jpg)" />
<!--<conditions>
<add input="{DB:{R:1}}" pattern="(.+)" />
</conditions>-->
<action type="Redirect" url="(Omniservice/foto/2017/02/02/18443151-810079.jpg)" />
</rule>
</rules>
</rewrite>
but it's not captured, despite testing with IIS says it's ok:
If I use <match url="(.*)" /> then I am redirected to the corrected new URL but nothing is showed anyway because it says there were too many redirects.
The rule works for me, but it redirects me to http://localhost/(Omniservice/foto/2017/02/02/18443151-810079.jpg) maybe you need to remove () from url attribute in action node? Do you not even see a 301 redirect with above rule?

IIS redirect from subdomain to path

Suppose i have real subdomain like this.
test.example.com, is it possible to write a rule for iis to redirect subdomain to test.example.com/SomeStaticPage.html
the rule should work for multi level subdomains, ie.
test.aaa.bbb.example.com should be redirected to
test.aaa.bbb.example.com/SomeStaticPage.html
Using URL Rewrite is very easy, in fact I'm wondering if I don't get the requirements exactly. (do you need different static pages per domain?, or is it really just to redirect to the same "SomestaticPage.html" while keeping the host name when no default page is specified?)
A simple rule like this would do what your question asks:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectToSomeStatic" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/SomeStaticPage.html" />
</rule>
</rules>
</rewrite>
<system.webServer>
</configuration>
Now, if you were looking for a more complete solution that allows you to specify a page based on the host name, then you could use a rewrite map where you add the host names that should be redirected and which page they should go to. For example the rule below will check if they are requesting "/" and the host name is in the rewrite map and will use that page to redirect, if it is not in the list then it will not do anything:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectToSomeStatic" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="{C:0}" />
<conditions>
<add input="{HostMap:{HTTP_HOST}}" pattern=".+" />
</conditions>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="HostMap">
<add key="test.example.com" value="/SomeStaticForTestExample.html" />
<add key="test.a.b.c.example.com" value="/SomePageForTestABC.html" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</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/

How to tell IIS7 to redirect traffic to asp.net

I have a url rewrite routine in global.asax, when the user types:
http://example.com/products/category/electronics
the server serves:
http://example.com/products.aspx?category=45
the problem is that IIS7 does not send this request to be handled by asp.net because the requested path has no folder in the directory structure.
How can I tell IIS to let this request be handled by asp.net without creating a directory for each product and category?
Technically you don't have IIS do this, you use URL Re-Writing to do it. This is a normal feature of ASP.NET MVC. However, it's possible in "classic" form-based asp.net as well.
There's an article about it here: http://msdn.microsoft.com/en-us/library/ms972974.aspx
Listed in the article are a number of ways to accomplish this, as well as a detailed explanation of writing an httpModule.
As a matter of fact, the article shows how to do EXACTLY what you're asking. Search on the page for "when a user visits /Products/Beverages.aspx" on the page and it'll take you right to the demonstration of how it works. (Instructions are higher up in the article.)
In a nutshell don't do this in your Global.asax. Use the IIS7 Url Rewriter instead:
Using the URL Rewrite Module
You have one of two solutions -
Use a URL Rewrite Map and map every category name to a category ID:
<rewrite>
<rewriteMaps>
<rewriteMap name="StaticRewrites">
<add key="/products/category/electronics" value="/products.aspx?category=45" />
<add key="/products/category/books" value="/products.aspx?category=46" />
<add key="/products/category/dvds" value="/products.aspx?category=47" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rewrite Rule 1 for StaticRewrites" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="False"/>
</rule>
</rules>
</rewrite>
Use a more "dynamic" rewrite:
<rewrite>
<rules>
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^products\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^([^=&]+)=([^=&]+)$" />
</conditions>
<action type="Redirect" url="products/{C:1}/{C:2}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^products/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="products.aspx?{R:1}={R:2}" />
</rule>
</rules>
</rewrite>
In the above case you'd need to change your products.aspx page to accept the category name, then in that page look up the actual integer ID of the category in the database.
Passing the rewrite responsibility to IIS7's URL Rewrite module (or any other rewriter such as Iconics IIRF or HeliconTech's ISAPI Rewrite) means that you can change these rules separately rather than burning them into your code. Burning these rules into your code means you have to redeploy every time you make a change.

Resources