IIS URL Rewrite removing part of path - url-rewrite-module

because a Software provider changed the URL of a Programm hosted in my IIS, I now have many broken links.
The URL used to be
https://example.com/#/subdomain/and/here/some_more
now it is
https://example.com/#/and/here/some_more
They just took away the first part of the path (subdomain)
I thought I could fix my links with an URL Rewrite rule.
I tried this:
<rewrite>
<rules>
<rule name="example" stopProcessing="true">
<match url="(https:\/\/example\.com\/#)(\/subdomain)(\/.*)" ignoreCase="true"/>
<action type="Redirect" url="https://example.com/#{R:3}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
This actually matches in the "Test Pattern" but it doesn't redirect.
I also tried using :
url="{R:1}{R:3}
but still no redirect is happening. I tried with different browsers with no success.
Thanks for the help

The number sign (#) delimits the URI from the fragment identifier. The client should never send this character uncoded. Instead, the client should only send the URI to the server (everything before #). So your client application (web browser) does not send anything after #, which we can see in the URL logged on the server. So what you need to do is to encode this character.
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

Related

URL rewrite module IIS - redirecting act differently when I change rule name

Recently I have been facing very strange issue with IIS rewrite module. So, I am trying redirect one of URL to a desired URL.
<rule name="Redirect to merchant dashboard login page" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAny">
<add input="{CACHE_URL}" pattern="*://*/*/login*" />
</conditions>
<action type="Redirect" url="https://dev.mydomain.com/apply/{C:3}/login" />
</rule>
In IIS manager this rule look like this:
also, just for the reference, in IIS manager there is option to test the pattern with my URL. So I give a screenshot of it how it look like:
As you can see, from the above picture my pattern extracting backreference correctly. So, when I hit a URL which match with this pattern I am expecting it should redirect me to the URL stated in IIS redirect URL part. However, when I hit a URL for example "https://dev.mydomain.com/rms/login" it append lots of "apply" word to the redirected URL which is so weird. Like the image below:
while debugging the issue from the browser I have found that when I hit the URL it hit the url multiple times and each time it add the backreference {c:3} to the redirected URL as a result it does not work.
What is so strange is the when I just change the rule name "Redirect to merchant dashboard login page" to "Redirect to merchant dashboard signup page" it works perfectly, which is - it redirect me to my expected URL https://circle.dev-youlend.com/apply/{C:3}/login
I am not understanding how can a change in Rule name make it working, how can I make it working even when I change the rule name?

IIS URL Rewriting - Change a querystring key and value into a route

I've recently implemented IIS URL Rewriting on one of my websites. I'm using URL Redirection rather than rewriting for everything. All of my static redirections are working perfectly, however, there's one specific type of dynamic redirection that I can't seem to make work.
I had old URL's that looked like this:
http://example.com/?tag=mytag
I'd like this to be redirected to the new URL format of:
http://example.com/tag/mytag
For these URL's the querystring key (tag) is known and fixed, however, the querystring value ("mytag" in the example above) is entirely dynamic and unknown in advance (so I don't believe it's therefore possible to use IIS Rewrite Maps).
Is is possible to add an IIS Rewrite rule that performs this kind of redirection for all possible querystring values that may be supplied?
Yes, the guts of the solution are below. Whats going on is...
1st condition means only apply this rule to the top level of the site. So http://example.com/?tag=mytag will redirect, whereas http://example.com/foobar/?tag=mytag wouldnt.
2nd condition is the magic. It only runs if a query param called tag exists, and the (.*) is a regex to grab the value for use in the new URL.
The action uses the value you grabbed in the 2nd condition referenced as {C:1}. appendQueryString does exactly what it says - set as appropriate. redirectType should be left as Temporary (HTTP response code 307) until your happy, then change it to Permanent (HTTP response code 301). Once you send a 301 response the client(/search engine) will potentially cache the response and not re-request from the server causing problems if you make a mistake.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect based on tag query value">
<conditions>
<add input="{REQUEST_URI}" pattern="$" />
<add input="{QUERY_STRING}" pattern="tag=(.*)" />
</conditions>
<action type="Redirect" url="tag/{C:1}/" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Redirect Subdomain URL to another SubDomain in IIS

I need to redirect a "fake" sub domain to a real subdomain in IIS 7.5. The reason is that the marketing department doesn't want to use the actual website address in print pieces.
Actual site URL:
reporting.usapartners.com
Marketing dept wants
report.usapartners.com (fake) to redirect to reporting.usapartners.com (real)
Again, report.usapartners.com does not exist, only reporting.usapartners.com exists
Here is what I tried
I added a binding in IIS, for the site reporting.usapartners.com. I added report.usapartners.com as the host name and used the reporting.usapartners.com IP address
Then I went into reporting.usapartners.com web.config and added this:
<rewrite>
<rules>
<rule name="report" stopProcessing="true">
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="report.usapartners.com" negate="false" />
</conditions>
<action type="Redirect" url="http://reporting.usapartners.com" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Looks like my solution creates an alias that cannot be redirected to.
Am I even approaching this problem correctly? Seems like it should be a simple problem to solve but perhaps not? Any ideas are welcomed and appreciated.
Thanks
I think you need to create a separate site with host bindings for report.usapartners.com (the fake site) in IIS. This is going to be a stub site (it will still need a path on the disk, but it's only going to have a web.config in there) which will just host a redirect rule.
Now click on HTTP Redirect for that site in IIS and tick Redirect requests to this destination and put http://reporting.usapartners.com in the textbox. Then tick Redirect all requests to exact destination (instead of relative to destination), don’t tick the next one and then choose Status Code Permanent (301).
If you want it to redirect and keep the subdirectories and/or query string, then you can change the contents of the textbox to be http://reporting.usapartners.com$S$Q. Note that there is no trailing slash in this case. The $S preserves the sub directories and the $Q preserves the query string.
Your rule is causing a redirect loop.
Observe what your rule does:
Match any given URL (this includes "/", "/something", "/something/another.html", etc.)
If the host name ISN'T "report.usapartners.com"
Redirect permanently the request to "http://reporting.usapartners.com"
So, as you see, as soon as the user is redirected to the reporting subdomain, it gets redirected again to reporting, because hostname isn't "report.usapartners.com".
The key here is the negate="true" attribute on the rule condition. Remove it or set it to false and you are good to go.
Edit:
You are almost there.
The real solution would be to change the host name on the rule to the desired host, keeping the negate true, so your rule would do:
Match any given URL (this includes "/", "/something", "/something/another.html", etc.)
If the host name ISN'T "reporting.usapartners.com"
Redirect permanently the request to "http://reporting.usapartners.com"
Code:
...
<add input="{HTTP_HOST}" pattern="reporting.usapartners.com" negate="true" />
...

IIS URL Rewriting At Initial Host Request

I am having a problem with an IIS7 Integrated Pipeline URL Rewrite.
For my particular scenario I need to rewrite/redirect part of the initial request as follows.
User enters http://savecontoso.com/files/123456789/somefile.html in the browser address bar.
User is redirected to http://savecontso.com/default.aspx?url= (results of url="default.aspx?url={R:1}")
This currently works as expected only if I create the initial request as such, http://savecontoso.com/default.aspx/files/123456789/somefile.html.
I must note that there is no actual directory of /files/ nor /123456789/ nor any file named somefile.html on the server. I simply need that entire path and filename appended to a query string.
This is my first day working with redirect/rewrite functions using IIS instead of page code behind and I have looked all around learn.iis.net, Google etc to no avail. I understand that rewriting takes place before page requests but for some reason my particular code requires a page request before firing the redirect.
I suspect it is because I am not triggering conditions at the initial request?
<rewrite>
<rules>
<rule name="1" stopProcessing="true">
<match url="(.*)(/files/\d+/.*html$)" />
<action type="Redirect" redirectType="Permanent" url="default.aspx?url={R:1}" />
</rule>
</rules>
</rewrite>
Most likely it does not work because of your match pattern:
the {R:1} will only match (.*) in your pattern, and will never match files/123...
URL in match pattern always starts with no leading slash: should be files/\d+... and not /files/\d+...
Try this one instead (works fine for me):
<rule name="1" stopProcessing="true">
<match url="^files/\d+/.*\.html$" />
<action type="Redirect" url="default.aspx?url={R:0}" redirectType="Permanent" />
</rule>

Problem with URL rewrite on IIS 7.5

I'm trying to use the URL Rewrite module for IIS 7.5 to redirect all HTTP requests to HTTPS for my ASP.NET website. The site works fine at the moment but forces the user to type the https:// in the address bar.
I followed the instructions in this article. Everything seems to be fine: I've tried putting the rule in the web.config and it shows up in the UI as it should; I've also done the reverse and can see the changes in the web.config when I use the UI to add the rule. I have RequireSSL unchecked for the site. Unfortunately I still just get a 404 when I try to hit the site via http://.
I've tried a few different action urls including {HTTP_HOST}/{R:1} and the one shown below.. nothing works.
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"
redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
</system.webServer>
I'm fairly new to this and pretty frustrated at this point. Seems like this should be a lot easier. Any advice would be appreciated, thanks..
Reposting from ServerFault as it's been sitting unanswered for a while.
HTTP Error 404. The requested resource is not found
Do you actually have binding for HTTP 80 port? Sounds like you do not have it (only HTTPS).
The reason I'm asking is the quoted text is the exact message that I would see if I request unknown to IIS domain (when there is no catch-all defined) or domain is not bound to the requested port.

Resources