Using URL rewrite with sub domains; download file fails - url

I currently have this rewrite URL rule in my web.config file
<rule name="Rewrite to qa" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^qa.golfgameskeeper.com$" />
</conditions>
<action type="Rewrite" url="qa/{R:1}" />
</rule>
it works great, makes qa.golfgameskeeper.com/qa work like qa.golfgameskeeper.com
However, when I try to download a file from a sub-directory of qa it seems to get confused
http://qa.golfgameskeeper.com/apps/iOS will list the file, but will not allow me to download it.
Even when clicking one the link above the rule re-writes the link to
qa.golfgameskeeper.com/qa/apps/iOS (cut and paste will work, not clicking the link)
Is there a way to modify this rule to allow what I'm trying to do? So, as I'm writing this I figured out what I am trying to do.
have
qa.golfgameskeeper.com -> qa.golfgameskeeper.com/qa (works)
and allow
qa.golfgameskeeper.com/apps/iOS to download the file without rewriting the URL twice (which is what I think it does).
Thank you,

You can add a rule before your rewrite rule to match on the apps subdirectory and do nothing, basically skipping the rewrite rule.
<rule name="Skip apps" stopProcessing="true">
<match url="^apps/(.*)" />
<action type="None" />
</rule>
<rule>
//your rewrite rule here
</rule>

Related

Replace %5E in URL using IIS Rewrite

I have the following problem:
I send a request via HttpResponse.Redirect method to a web application:
http(s)://localhost/application?arg1=abc^def
This works pretty fine on IE/Edge/Chrome/Firefox. But on Safari I get the problem that the request is sent as following encoded string:
http(s)://localhost/application.aspx?arg1=abc%5Edef
The application does not like/accept that and I am not able to perform changes on the application.
Is it possible to rewrite the URL on the webserver where the application is hosted by IIS Rewrite module somehow?
I tried already to create a rule with following regex pattern:
^application?(.\*)%5E(.\*)
and the following action:
Rewrite URL:
application.aspx?{R:1}%5E{R:2}
...but this does not seem to work (although the regex pattern has passed the "Test Pattern" test)
Many thanks in advance!
This is because there is a problem with your rewrite rules. The following configuration will rewrite http://localhost/application.aspx?arg1=abc%5Edef to http://localhost/application?arg1=abc^def:
<rewrite>
<rules>
<rule name="TEST" stopProcessing="true">
<match url="(application.aspx)" />
<action type="Rewrite" url="http://localhost/application?{C:1}^{C:3}" appendQueryString="false" />
<conditions>
<add input="{QUERY_STRING}" pattern="(.*)(%5E)(.*)" />
</conditions>
</rule>
</rules>
</rewrite>

IIS URL Rewrite with wild card

I need to change current URL pattern (hotels/{City}) to more SEO friendly one villa-{city} and I need to add a wild card for cities. Any city name should allow by the site and previous URL pattern need to rewrite as new pattern. I tried the below rewrite and its working for redirection but it effects on other URL patterns like "hotels/{name}/{code}" too. How can I overcome this redirection effecting on other patterns?
Can someone help me with this?
I would like to know how can we determine whether we use {R:0} Or {R:1} for conditions and the purpose of adding "^" to patterns
<rule name="HotelToVilla" stopProcessing="true">
<match url="hotels/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="localhost:23617" />
</conditions>
<action type="Redirect" url="http://localhost:23617/villa-{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>

IIS URL Rewrite multiple parameters

I am really confused about the URL rewrite interface. I dont understand what i need to do.
I have an url as:
www.example.com/diretory/subdirectory/index.html?param1=1&param2=2&param3=2&param4=7
I want to hide this url in a <a>-href tag, which displays "Example Tag".
When the url is requested it should rewrite it to
www.example.com/program/location/year/vacancie
I allready tried this:
<system.webServer>
<rewrite>
<rules>
<rule name="ProgramRewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\?([^/]+)&([^/]+)&([^/]+)&([^/]+)" />
<action type="Rewrite" url="www.example.com/program/location/year/vacancie" logRewrittenUrl="true" />
<conditions>
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
In the URL Rewrite Interface the Test Pattern said it is working and gets:
?param1=1&param2=2&param3=2&param4=7
param1=1
param2=2
param3=2
param4=7
I checked the log url rewrite as well, but in my logs it is not shown.
2017-03-20 16:29:24 192.168.253.146 GET /diretory/subdirectory/index.html param1=1&param2=2&param3=2&param4=7 88 - 192.168.253.146 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/56.0.2924.87+Safari/537.36 - 304 0 0 4
ps: the urls are not working and only for illustrative purpose.
The match URL only matches the URL and does not take the querystring into account. You will need to add a condition for this. Also do you want to rewrite this (so the server internally executes the new URL) or redirect (so the server will request the browser to go to the new URL and URL changes in address bar). In case you want to rewrite you should not add the domain again, in case you want to redirect add http:// as well. Assuming a rewrite is what you want use below rule:
<rule name="ProgramRewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="/program/location/year/vacancie" logRewrittenUrl="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="([^/]+)&([^/]+)&([^/]+)&([^/]+)" />
</conditions>
</rule>

Using IIS URL Rewrite to limit content to one of two domains results in redirect loop

I have two domain names for my website, one of which is the ubiquitous cookie-less static content domain. One name is merely an alias for the "main" name (using CNAME), so without additional configuration, any resource could be accessed through either domain. For security reasons in addition to just wanting navigation to be intuitive, I want to make sure that static content and only static content is available through the static domain, and in particular, that static content is NOT available through the main domain.
I accomplished the latter using IIS's URL Rewrite, with the following rule:
<rule name="StaticContentRes" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^maindomain$" />
<add input="{REQUEST_URI}" pattern="^/res/.*$" />
</conditions>
<action type="Redirect" url="https://staticdomain/{R:0}" />
</rule>
This works perfectly fine, and ensures requests for certain static content (all located in the "res" directory) are served only through staticdomain. However, I tried to further enforce that staticdomain be used ONLY for requests to that directory:
<rule name="RegularContent" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^staticdomain$" />
<add input="{REQUEST_URI}" pattern="^/res/.*$" negate="true" />
</conditions>
<action type="Redirect" url="https://maindomain/{R:0}" />
</rule>
Adding this second rule results in an infinite redirect loop whenever either domain is used to serve that directory. I'm completely stumped, as I thought the negate="true" logic was perfectly clean and workable. What can I do so that staticdomain requests are let through only if they're to the "res" dir, without incurring this loop?
As often seems to be the case, I found the issue immediately after posting this question. Having stopProcessing="true" in the first rule meant that the second rule would never be used, since the match patterns in the first rule catches everything. I didn't realize that the added conditions are not taken into account when deciding whether a request has officially matched a rule (for the purposes of stopProcessing), just the match pattern.

IIS URL Rewrite not working with query string

I thought this was pretty straightforward, but it refuses to work. The old URL is
http://www.site.com/?q=node/17
It needs to redirect to http://www.site.com. I don't need to worry about wildcards, this is the only query string parameter I need to worry about. The rule I wrote looks like
<rule name="Node17" patternSyntax="ExactMatch" stopProcessing="true">
<match url="http://www.site.com/?q=node/17" />
<action type="Redirect" url="http://www.site.com" appendQueryString="False" />
</rule>
I can test the pattern inside of IIS and it matches, but when I hit the URL in a browser it doesn't redirect. Any thoughts?
As described in Microsoft's documentation:
It is important to understand how certain parts of the URL string can
be accessed from a rewrite rule.
For an HTTP URL in this form:
http(s)://{host}:{port}/{path}?{querystring}
The {path} is matched against the pattern of the rule. The
{querystring} is available in the server variable called QUERY_STRING
and can be accessed by using a condition within a rule.
Rule conditions allow defining additional logic for rule evaluation...
Rule conditions are evaluated after the rule pattern match is successful.
In the URL you wanted to rewrite as a redirect, your {host} = "www.site.com", {path} = "" and {querystring} = "q=node/17". So the {path} part in the URL you wanted to redirect is actually empty, and the rule you used in your question was matched against it and did not match.
Your solution is indeed valid, so I'll quote it here:
<rule name="Node17" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="q=node/17" />
</conditions>
<action type="Redirect" url="http://www.example.com" appendQueryString="False" />
</rule>
Of course I figured it out soon after I posted. This does it, not really sure why the exactmatch wasn't working though.
<rule name="Node17" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="q=node/17" />
</conditions>
<action type="Redirect" url="http://www.site.com" appendQueryString="False" />
</rule>

Resources