IISRewrite encoding of %2f in path - url-rewriting

So I have a path like this (I am using webforms):
folder/first/second/
which has the following rewrite rule behind it
<rule name="firstLevel">
<match url="^folder/([^/]+)/([^/]+)/?$" />
<action type="Rewrite" url="firstSecond.aspx?param1={R:1}&param2={UrlEncode:{R:2}}" />
</rule>
my issue is when I try and pass the following:
folder/first/sec%2fond/ (I want R2 to be sec%2fond where I will urlDecode it back to the page. for a result like "sec/ond".
However my rule keeps taking it as though I want
folder/first/sec/ond
Requested URL
http://localhost:85/research/first/sec/ond/
Physical Path
\rewritetest\folder\first\sec\ond\
Thanks for any assists..

I found a working solution to this issue for anyone else.
<rule name="secondLevel">
<match url="^folder/(.+)/(.+)/?$" ignoreCase="true" />
<action type="Rewrite" url="firstsecond.aspx?param1={C:1}&param2={C:2}" />
<conditions>
<add input="{UNENCODED_URL}" pattern="/folder/([^/]+)/([^/]+)/?$" />
</conditions>
</rule>

Related

When a dot exist in the middle of the URL, the rewriter works wrong

I have a project in asp net core 2.0.
I Have stablished the next Rule in the web.config:
<rewrite>
<rules>
<rule name="wwwroot" >
<match url="([\S]+[.](js|css|png|gif|jpg|jpeg))" />
<action type="Rewrite" url="wwwroot/{R:1}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
When i acces to the url: www.foobar.com/lib/chart.js/dist/Chart.js for some reason the iis try to finde Chart.js in the path: wwwroot/lib/chart.js ignoring the last part of the url (/dist/Chart.js) when im expecting to take wwwroot/lib/chart.js/dist/Chart.js
Try to modify your regexp like this: ([\S]+[.](js|css|png|gif|jpg|jpeg))$ it should consider file extension if they at the end of your url
EDIT
To your rule you should exclude urls starting from wwwroot
<rule name="wwwroot" >
<match url="([\S]+[.](js|css|png|gif|jpg|jpeg))" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" negate="true" pattern="^/wwwroot" ignoreCase="true" />
</conditions>
<action type="Redirect" url="wwwroot/{R:1}" logRewrittenUrl="true" />
</rule>

IIS URL rewrite - with dynamic files

I am using this answer https://stackoverflow.com/a/13861795/848513 which works great for static files, which no longer exist on my site, to forwards to new file locations.
But I also have some dynamic URL's that need to be forwarded to new locations, and the following code doesn't seem to work:
Works:
<rule name="SpecificRedirect50" stopProcessing="true">
<match url="aboutus.php" /> <!-- static URL -->
<action type="Redirect" url="/about-us" redirectType="Permanent" />
</rule>
Does not work:
<rule name="SpecificRedirect1" stopProcessing="true">
<match url="topic.php?id=39" /> <!-- dynamic URL-->
<action type="Redirect" url="/folder/?id=520" redirectType="Permanent" />
</rule>
The error i get when trying to go to www.site.com/topic.php?id=39 is a 404 Not Found error - ie, it isn't being filtered by the rewrite script.
What should the format be?
Thanks
OK, found answer - this format works:
<rule name="SpecificRedirect1111" stopProcessing="true">
<match url="^topic\.php$" />
<conditions>
<add input="{QUERY_STRING}" pattern="^id=39$" />
</conditions>
<action type="Redirect" url="/folder/?id=520" appendQueryString="false" redirectType="Permanent" />
</rule>

How do I ignore the .aspx extension when using a rewrite map?

I have a rewrite map with ~100 old URL to new URL rows:
<rewriteMaps>
<rewriteMap name="OldSiteRewriteMap" defaultValue="">
...
<add key="/a/b/oldpage" value="/x/y/newpage" />
...
</rewriteMap>
</rewriteMaps>
I have a rewrite rule that uses that map to redirect URL's (we've just launched a new website and we're mapping the URL's from the old site to the corresponding URL on the new site):
<rule name="Redirect old site URLs">
<match url=".*" />
<conditions>
<add input="{OldSiteRewriteMap:{PATH_INFO}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" redirectType="Permanent" appendQueryString="false" />
</rule>
When I visit http://example.com/a/b/oldpage (the old site URL) I get redirected to http://example.com/x/y/newpage - great! But when I visit http://example.com/a/b/oldpage.aspx -note the .aspx extension - I don't get redirected.
How can I tell my rewrite rule to ignore the ".aspx" extension when it's trying to map URL's using the rewrite map?
A coworker suggested the following - using a regex in the "match url" to capture everything except a possible trailing ".aspx" and then passing the capture to the rewrite map in lieu of PATH_INFO or any of the other server vars. Seems to work pretty well.
<rule name="Redirect old site URLs">
<match url="^(.+?)(\.aspx)?$" />
<conditions>
<add input="{OldSiteRewriteMap:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" redirectType="Permanent" appendQueryString="false" />
</rule>

IIS 8.0 Directory HttpRedirect

Is there a way in IIS to redirect the following request:
http://mysite/report1/img/logo.png to http://mysite/myapplication/report1/images/logo.png for ALL images in the img directory without having to explicitly map them individually?
Additional requirement- I have THOUSANDS of reports on a drive mapped to the 'report1' virtual directory- each with their own 'img' directory- so there is no reasonable way to use IIS manager to map those directories individually either.
I'm looking to see if there is some way to add a wildcard (or other) HttpRedirect in the IIS server web.config file to correctly map all the images for all the reports. I tried:
<add wildcard="*res/img/" destination="/reporter/content/images/reportimages" />
But that seemed to have no effect.
EDIT: Some more research shows that using the URL Rewrite module might work... but so far I haven't gotten it to work.
My rule looks like this (in web.config):
<rules>
<rule name="Redirect rule1 for ImageRedirect">
<match url=".*" />
<conditions>
<add input="{ImageRedirect:{REQUEST_URI}}" matchType="Pattern" pattern="/res/img/(.+)" ignoreCase="true" negate="false" />
</conditions>
<action type="Redirect" url="{HTTP_HOST}/reporter/content/reporterimages/{C:1}" appendQueryString="false" />
</rule>
</rules>
You were on the right track using the URL Rewrite module.
The most simple rule in your case would be:
<rule name="Rewrite images" stopProcessing="true">
<match url="^/report1/img/(.+)$" />
<action type="Rewrite" url="/myapplication/report1/images/{R:1}" />
</rule>
It does check if the requested url matches ^/report1/img/(.+)$ and if yes, trigger a rewrite to your new folder.
If you want to use a Redirect instead:
<rule name="Redirect images" stopProcessing="true">
<match url="^/report1/img/(.+)$" />
<action type="Redirect" url="/myapplication/report1/images/{R:1}" />
</rule>
(If you don't specify it, by default a Redirect is permanent (301))

ISAPI PHP REWRITE

A friend is having me figure out a way to modrewrite on his windows server. He is running IIS 6 and Isapi is about the only thing I can find. I am not familiar with it and have read some of the documentation, but can't quite wrap my head around it. He is wanting to rewrite these URLS to make them clean
www.domain.com/cat.php?CTGID=####
and
www.domain.com/pp.php?ID=##
How would I go about rewriting these two URLS to make them Clean in ISAPI. I have installed it on the Windows Server and do I put these rules in IISF.ini in his website folder or put these codes in the master IISF file? Any Help would be greatly appreciated!
Rewrite rules go in a web.config file or in the ApplicationHost.config.
As described in http://learn.iis.net/page.aspx/465/url-rewrite-module-configuration-reference/
Examples
<rewrite>
<rules>
<rule name="Force WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" />
</rule>
<rule name="Redirect from blog">
<match url="^blog/([_0-9a-z-]+)/([0-9]+)" />
<action type="Redirect" url="article/{R:2}/{R:1}" redirectType="Found" />
</rule>
<rule name="Rewrite to article.aspx">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="article.aspx?id={R:1}&title={R:2}" />
</rule>
</rules>
</rewrite>
For IIS, if the ISAPI_Rewrite module is installed, $_SERVER["IIS_UrlRewriteModule"] is set and contains the version number of the module. So you can check for the existence of this server variable.

Resources