setting rule in IIS url rewrite module - asp.net

I looking for help making URL rewrite rule for redirecting all urls that start with
http://localhost:13080/saCore/ws/messagebroker[Here something that changes]
to
http://localhost:13080/saCore/ws/messagebroker/MessageBroker.asmx[Here something that changes]
I just to need to push MessageBroker.asmx in the middle of the path after messagebroker part.
Thanks for help.

This should do the job, you can add it directly to applicationHost.config or use IIS Manager to create the rule:
<rule name="RedirectBroker" stopProcessing="true">
<match url="^saCore/ws/messagebroker(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^localhost$" />
<add input="{SERVER_PORT}" pattern="^13080$" />
</conditions>
<action type="Rewrite" url="/saCore/ws/messagebroker/MessageBroker.asmx{R:1}" appendQueryString="true" />
</rule>

Related

IIS url rewrite rules to hide subfolders

I have 2 .net API projects, servce1 and service2.
I would like to put them both under the same domain (site). I can ensure the APIs routing not overlapping.
I deployed the projects as:
Default Web Site/
service1 (raw url: http://mydomian/service1/api/ONE)
service2 (raw url: http://mydomian/service2/api/TWO)
What I want to achieve is that I can use http://mydomain/api/ONE or http://mydomain/api/TWO to access both service1 and service2 APIs without the subfolders in the URL.
I tried using URL rewrite module I got it works for one, but I can not get the second one work, with the same settings. See the setting file below.
I tried moving the rules orders. no luck.
<rewrite>
<rules>
<clear />
<rule name="first" stopProcessing="false">
<match url="(.*)ONE(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{REQUEST_URL}" pattern="(.*)/api/ONE(.*)" />
</conditions>
<action type="Rewrite" url="/Servuce1/api/ONE{R:2}" logRewrittenUrl="true" />
</rule>
<rule name="seconds" stopProcessing="false">
<match url="(.*)TWO(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="(.*)/api/TWO(.*)" />
</conditions>
<action type="Rewrite" url="/service2/api/TWO{R:2}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
What is wrong with my settings?
What are the alternatives?
Should the stopProcessing be set to "true", so that the second rule is not applied when the first rule gets matched?

Redirect rule http to https not working when using full url

My azure cloud service has both http and https enabled in config files.
I tried adding this rule in my web.config
<rule name="RedirectHTTPToHTTPS" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
if I use http://mysite.kom ,it is correctly redirecting to https://mysite.kom
But if I type http://mysite.kom/other/default.aspx it is redirecting to http://mysite.kom/other/default.aspx instead of https://mysite.kom/other/default.aspx
I added GlobalFilters.Filters.Add(new RequireHttpsAttribute()); in my application_start event.But still "http://mysite.kom/other/default.aspx" is not getting redirected to "https://mysite.kom/other/default.aspx"
I tried all the similar questions in stackoverflow.I even moved the this rule to top of my rules config. Any suggestions to make this working
To resolve this , I added
if (!HttpContext.Current.Request.IsSecureConnection && !HttpContext.Current.Request.Url.Host.Contains("localhost"))
{
Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"]
+ HttpContext.Current.Request.RawUrl);
}
to Application_BeginRequest event inside my global.asax.cs file
Here is what I use:
<rewrite>
<rules>
<rule name="redirect HTTP to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{WARMUP_REQUEST}" pattern="1" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
While you aren't preserving the query string, it still seems like you have everything necessary to get http->https redirection. That makes me wonder if browser caching is causing you problems? I have had the issue before where I didn't think it was working because I hit the site before enabling http->https redirection and it turned out to be my browser serving up the page from cache.
I wrapped this in a site extension, and it uses the above rewrite rule, so I know it works:
http://www.siteextensions.net/packages/RedirectHttpToHttps/

Redirect Non-WWW to WWW (except one spesific subdomain)

On my Windows 2012 server I have installed the URL Rewrite module in IIS. I have followed this guide to redirect non-www to www in web.config:
http://www.surfingsuccess.com/asp/iis-url-rewrite.html#.VF6GBid0yAU
So far, so good!
The only problem is that we also host the subdomain "api.example.com". This API stops working when I apply the code in web.config.
My question is: What is wrong with the code below? Why does the subdomain stop working when I try to redirect non-www to www, except "api.example.com"?
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
<add input="{HTTP_HOST}" pattern="^api\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
I have similar check and I have same rule but have specified differently in pattern.
My suggestion is to enable Failed request tracing to check URL rewrites.
http://www.iis.net/learn/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules
Edit: Updated rule.
<rewrite>
<rules>
<rule name="non-root" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(www.example.com)$" negate="true" />
<add input="{HTTP_HOST}" pattern="^(api.example.com)$" negate="true" />
<add input="{HTTP_HOST}" pattern="^(example.com)$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
Actually, there was nothing wrong with my rewrite code. The reason why the API stopped working was because it had a referance to the top domain of the site (example.com), this caused a conflict with the rewrite code. After changing the referance to the www -version of the doman (www.example.com) everything worked fine.

Rewrite rule to WWW except when on localhost

For SEO optimization reason, I get all requests to http://Example.com redirected to http://www.Example.com. The problem is that when working on local, requests to localhost get redirected as well.
I tried the suggestion in this Rewrite rule to HTTPS except when on localhost answer with no luck.
Here is my actual redirection rule located in Web.Config (hopefully it can help someone that is looking for Rewrite rule to WWW):
<system.webServer>
<rewrite>
<rules>
<rule name="redirect example.com to www.example.com">
<match url="^(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
Any help?
You can do a rule as follows
<rule name="redirect example.com to www.example.com">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www.*" negate="true" />
<add input="{HTTP_HOST}" pattern="localhost" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" />
</rule>
</rules>
It combines two conditions with "MatchAll" where the first input is similar to one you already have (you can use yours if you want) and the second one is to check for localhost. Note, that you would probably need to use {R:0} and I also changed match url to .*
You can also use different web.configs (debug and release). Read more here: http://msdn.microsoft.com/en-us/library/vstudio/dd465318(v=vs.100).aspx

How can I create a URL rewrite rule for a domain-only request?

I am trying to create a URL rewrite for a site that has multiple domains. For example one of the domains is mydomain.com and if a user puts www.mydomain.com in their browser and does not specify a page I want to rewrite the URL to call www.mydomain.com/landingpage.aspx?cat=1&sol=4.
If the user calls anything else such as www.mydomain.com/somepage.aspx this rule should be ignored.
I have installed URL Rewrite 2.0 on the server 2008 R2 machine we have and I have added this rule to the web.config.
<rewrite>
<rules>
<rule name="mydomain.com" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?mydomain.com" />
<add input="{PATH_INFO}" pattern="^$" negate="true" />
</conditions>
<action type="Rewrite" url="\landingpage.aspx?cat=1&sol=4" />
</rule>
</rules>
</rewrite>
I am using the {PATH_INFO} of ^$ so that if anything other than just a call for the domain occurs this should ignore it I think. However it does not work.
I am using .NET 4.0 on the site.
Can someone tell me what I am doing wrong please?
You are looking for following rule:
This will check if URL is empty i.e. no page is specified using match url=^$ - empty string and redirect to specific page.
<rule name="Redirect to specific page" enabled="true" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?mydomain.com$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/landingpage.aspx?cat=1&sol=4" />
</rule>

Resources