Need to redirect folder URL /media/default to Page URL /media/default/image - url-rewriting

IIS rewrite rule is not working.
I have tried adding the following rule for redirect but it's not working. Can anyone help me on this as I am not familiar with rewrite rules.
<rule name="test" patternSyntax="ExactMatch" stopProcessing="true">
<match url='/media/default' />
<action type="Redirect" url="/media/default/image" />
<conditions>
</conditions>
</rule>

Related

IIS rewrite rule to get domain without tld

Looking to create a working IIS rewrite (not a redirect) rule to rewrite http{s}://www.client1.com/page1.htm to https://client1.mysite.com/myapp/client1/page1.htm - I just can't get it working. Driving me a little mad.
My main problem is getting the domain name WITHOUT the TLD part returned as an R{x} or C{x} parameter...
Note the client name is in two places in the target url - AND Note that the original url could be client2.com or client3.org or client4.net
This rule will rewrite it for you:
<rules>
<rule name="Rewrite clients domains" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?([a-zA-Z0-9\-\.]+)\.(\w+)$" />
</conditions>
<action type="Rewrite" url="{MapProtocol:{HTTPS}}://{C:2}.mysite.com/myapp/{C:2}/{R:0}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>

URL Rewrite troubleshoot

We have our URL rewrite as followed. I tested the pattern in IIS 8.5 URL Rewrite and the patterns matched as what we wanted. However, it's 404 every single time. Will someone take a look and see what is wrong with my URL rewrite? Thank you.
URL: devbox.mysite.com/article/how-to-use-onedrive
Pattern: (.*)/article/(.*)$
Action Rewrite URL: {R:1}/article/?artID={R:2}
Currently the page shows 404. Below is the XML code in web.config file.
<rule name="Article-rewrite" enabled="true">
<match url="(.*)/article/(.*)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="{R:1}/article/?artID={R:2}" appendQueryString="false" />
</rule>
Try this:
<rule name="Article-rewrite" enabled="true">
<match url="article/(.*)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="article/?artID={R:1}" appendQueryString="false" />
</rule>
I've removed the domain from the match url, as you're not switching the domain.

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