Web Config Rewrite/redirect - asp.net

I am working on an existing project that I did not create.
In the web.config file, there are a number of redirects, setup in the following way:
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/findyourlocalbranch/" value="/find-your-local-branch/" />
</rewriteMap>
</rewriteMaps>
It seems to me that a rewrite rule is being used to do these redirects, but as I said, I did not write this and am not in a position to change this. I am not sure if this is right or wrong.
My problem is that I need to implement the following redirect:
<add key="/need-a-loan/.htm" value="/need-a-loan/" />
but this brings the whole site down - I suspect due to a syntax error in this line.
I suspect the error is the '.' after the '/'
My question is, what can I do about it - there is nothing on the internet that I can find about this.
Basically, I want to redirect this one, specific URL to the given URL???

I can't say whats going on with the map, as without the corresponding rule I'm blind.
But, this rewrite rule will redirect from /need-a-loan/.htm to /need-a-loan/.
<rewrite>
<rules>
<rule name="Need-A-Loan" stopProcessing="true">
<match url="need-a-loan/.htm" />
<action type="Redirect" url="need-a-loan/" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Note: doesn't use the map, so you'd remove that entry.
That will give you a 301. If you want a different type, you can adjust the rediectType. E.g. Temporary will give you a 307.

Related

Website deployed on IIS Server displays a blank page

The main problem is specified in the title; however, it is a little more nuanced than that.
I cannot give away the exact address, but the URL has the syntax: https://name.mywebsite.com.
If I go to that exact address, the page will load as intended. If I try to be more specific about where exactly I want to go on the page - let's say to: https://name.mywebsite.com/home - I get a blank page.
Curiously enough, after I go to https://name.mywebsite.com, all the other addresses work as well and either redirect me to that address or display the site that they are supposed to show. I figured out that some necessary Cache-Data and data for the local storage are not set if I don't call the site over the mentioned address.
All of that wouldn't be much of a problem, since only the first call of the website is affected. However, most users of the site will first call it with a QR-Code (which leads to a specific sub-page of the site), and it wouldn't be very nice if they are greeted with a blank page.
Also, it is a .NET Core App with Angular Frontend.
I can reproduce the error by clearing my cache and cookies. The necessary data is no longer set and all domains will display a white page - except https://name.mywebsite.com - again.
I think I understood the problem, but I cannot seem to come up with a solution. I tried some changes in the IIS Manager on the server, but to no avail. My main bet was to apply an inbound rule with the URL Rewrite function, but that did not work.
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect from first shop open" enabled="false" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="https://name.mywebsite.com/" appendQueryString="true" logRewrittenUrl="true" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
I also tried that rule with all possible Redirect types. Those rules do alter the page, but not in the way I want it. Instead of the blank white page I get an error that the redirect failed (with the hint that it is probably due to a lack of cookies). That error applies to all pages, even https://name.mywebsite.com. So I have no way of accessing the website now.
I also tried to find any other configuration that might prevent other URLs from loading the data, but I did not find anything like that either (or I simply looked at the wrong places).
Edit: /home would be a route in Angular. Also, the only rewrite rule that exists and that I tried is the one shown in the picture (which I updated so that it fits better with the question). I made some changes to the question in general.
As it turns out, I simply had to make some changes to the rule I posted in the question. Changing the Redirect to a Rewrite that targets /index.html and adding some conditions (which should be adapted to your personal needs) did the trick and the website no longer shows a blank page.
The link from Lex Li in the comments from the questions angular.io/guide/deployment#fallback-configuration-examples and the link https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#accessing-url-parts-from-a-rewrite-rule helped me building the correct rule.
<rewrite>
<rules>
<rule name="Angular Routes" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern=".*/(home|item.*)" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" logRewrittenUrl="false" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name=".*" />
</rewriteMaps>
</rewrite>

Regex to redirect a url using url rewrite

OK, I'm wondering if someone can lend a hand with a regex I'm trying to write.
Basically, what I want to do is use IIS urlrewrite module to do a redirect to a specific URL if the user accesses a URL on another site. The only catch is I have to also capture a bit of the query string, and move it into the redirect.
so here is the input, the URL that a user may access would look like:
https://of.example.com/sfsv3.aspx?waform=pro&language=en
I want to match that URL (either http or https, case insensitive), and capture from it also one piece of information, the two letter language code. then the url i want to forward the user to looks like:
http://example.com/ca/en/ppf
(where en is replaced by whatever i captured above)
So, I'm working with IIS Rewrite module, and I've gotten my input data and regex in, so far the regex pattern I have is this:
https?://of.example.com/sfsv3.aspx\?waform=pro&(language=(..))
so basically i'm matching the whole string, plus a group and a subgroup for language and it's code. in the IIS test pattern dialog, this is working.
I get the following
{R:1} language=en
{R:2} en
great! so then my IIS rewrite rule should look like this to redirect the user:
<system.webServer>
<rewrite>
<rules>
<rule name="test" stopProcessing="true" enabled="true">
<match url="https?://of.example.com/sfsv3.aspx\?waform=pro&(language=(..))" ignoreCase="true" />
<action type="Redirect" url="http://www.example.com/ca/{R:2}/ppf" />
</rule>
</rules>
</rewrite>
</system.webServer>
this all seems right to me. however, the redirect is not occurring. it seems to have a problem with the part \? (an escaped question mark to mark the start of the query string). if this is included, then the redirect simply does not happen.
Can you help me figure out how to get it work?
for the record, I figured it out. this is a special case of regex, running it inside a web.config as part of a urlrewrite action. in that case, you can't handle the query string with simple regex, you have to put in conditions on the query string. Here's what eventually ended up working:
<system.webServer>
<rewrite>
<rules>
<rule name="redirect" stopProcessing="true" stopProcessing="true" enabled="true">
<match url="sfsv3.aspx\.aspx" ignoreCase="true"/>
<conditions>
<add input="{QUERY_STRING}" pattern="subject=PROPRCH" />
<add input="{QUERY_STRING}" pattern="(..)/subject" />
</conditions>
<action type="Redirect" url="https://www.example.com/ca/{C:1}/forms/ppf" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
</system.webServer>

How to preserve query parameters using IIS HTTP wildcard redirects?

I'm trying to configure redirects to prepare the switch from an ASP to an MVC website. The IIS wildcard redirects are working fine, but I cannot figure out how to preserve the query parameters.
Example:
<system.webServer>
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Permanent">
<add wildcard="/EmailAddress-Verify.aspx" destination="https://foo.com/account/email-verify$Q" />
</httpRedirect>
</system.webServer>
I read that $Q will preserve the query params but it's not working. Google could not find anything on that issue.. is there anything I'm missing here?
Thanks!
So this configuration was working perfectly! I just had to clear my browser cache since the redirection is Permanent and the browser 'remembered' my previous configuration while I was playing with the config file. My bad!
Y̶o̶u̶ ̶h̶a̶v̶e̶ ̶e̶x̶a̶c̶t̶D̶e̶s̶t̶i̶n̶a̶t̶i̶o̶n̶=̶"̶t̶r̶u̶e̶"̶.̶ However, more importantly, when you want to have more granular control with redirects, you're much better off using the URL Rewrite module. Your bit would be something like the following (not tested and quickly written up):
<rewrite>
<rules>
<rule name="Email Address Verify Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="^EmailAddress-Verify.aspx(.*)$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://foo.com/account/email-verify{R:1}" />
</rule>
...
</rewrites>
Oh, now that I think about it, you could probably just do a straight up pattern without the following wildcard and just appendQueryString="True" in the action.

ASP.NET / IIS7 Url Rewrite maps not working

I've followed the instructions Learn IIS's webpage for adding static redirects with a rewrite map for my asp.net application.
The following is the config:
<rule name="Redirect rule1 for Information" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{Information:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="true" />
</rule>
And
<rewriteMaps>
<rewriteMap name="Information">
<add key="/Information/CorporateSales.aspx"
value="/KB/Information/CorporateSales" />
<add key="/Information/ComputerRepair.aspx"
value="/KB/Information/ComputerRepair" />
</rewriteMap>
</rewriteMaps>
This was even originally created by the wizard in IIS's manager for using rewrite maps.
So the idea is that /Information/CorporateSales.aspx --> /KB/Information/CorporateSales with a 301 redirect (MOVED PERMANENTLY).
However I'm just getting the original aspx page (Which we're removing later) loading. I've even deleted the file incase it was defaulting to an existing resource, and with that i just get a plain 404 without the redirect.
Anyone have an idea?
Let me clarify something:
Rewrite module works, it's installed and running. My standard regex rules work nicely. But my rewrite map does not.
This article http://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module and code below worked for me.
<rewrite>
<rules>
<rule name="Redirect rule1 for RedirectURLs">
<match url=".*" />
<conditions>
<add input="{RedirectURLs:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="RedirectURLs">
<add key="/privacy.php" value="/privacy" />
</rewriteMap>
</rewriteMaps>
</rewrite>
I was having a similar problem and found this question. It took me a little while, but I was able to figure out what the problem was.
My rewriteMap contained the urls "/Default2.aspx" and "/Dashboard.aspx".
When I would go to Default2.aspx, I would get a 404 rather than get redirected to Dashboard.aspx as expected.
The issue I found was that on my machine, the application was running in a subdirectory. The rewriteMap paths would only work if I used the full path (including the application folder), e.g., "/TestSite/Default2.aspx".
So I could have added duplicate entries in my rewriteMap to account for application directories on developer machines, but that seemed messy. I looked at the other rewrite rules in the application that did not have this issue and I noticed that they were using the {REQUEST_FILENAME} variable, rather than {REQUEST_URI}. So I switched the rule to use {REQUEST_FILENAME} and remove the first slash from the urls in my rewriteMap.
Do you have Url rewriting installed as part of IIS7/7.5? This is not installed by default. Also, make sure your app pool is set to integrated pipline mode, no classic.
Edit
From this:
http://learn.iis.net/page.aspx/469/using-rewrite-maps-in-url-rewrite-module/
This only thing I see that you're doing is adding the 'stopProcessing' attribute. Have you tried removing that?
Previously I had same problem as you described.
Could you update your code to
<match url="(.*)" />
and I hope you aware,
<add input="{Information:{REQUEST_URI}}" pattern="(.+)" />
this condition will capture full URL except the domain.
example on this url:
www.example.com/Information/CorporateSales.aspx
it will check matching condition of
Information/CorporateSales.aspx on rewriteMap
and for sure it wont be match with this url
www.example.com/old/Information/CorporateSales.aspx
Did you reset the app pool and the iis site ?
In some cases it can take up to 72 hours (iirc) to propagate throughout the world.

IIS7 Rewrite Rule not working

I have created a this rule:
<rewrite>
<rules>
<rule name="ImageRedirect" stopProcessing="false">
<match url="^(.*)/(.*)/" />
<action type="Rewrite" url="http://www.lrgimages.com/ImageRewrite.aspx?=img={R:2}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
I keep getting a 404.0 message like the rule is not working or IIS is not picking it up. In the test parttern section for the rule, the pattern tests fine. If I go directly to the http://www.lrgimages.com/ImageRewrite.aspx that page loades, but not when I try: http://www.lrgimages.com/TestImage
any thoughts?
Update: I figured it out. It does not take into acount hte http://www.DomainName.com when rewriting a url. Redirects work this way since htat is what is is really doing. I am used to other rewrite engines not taking into account the http://www.DomainName.com . Thanks all you lead me in the right direction.
I don't think "/TestImage" matches ^(.*)/(.*)/ ...

Resources