How to rewrite one image with another in IIS using URLRewrite - iis

Here is my problem, I need to rewrite one image with another (don't ask why).
Simple URLRewrite rule is not working.
Every time someone fetch this URL: http://example.com/images/001.jpg
IIS should show 002.jpg (http://example.com/images/002.jpg)
web.config:
<rules>
<rule name="img1" stopProcessing="true">
<match url="images/001.jpg" />
<action type="Rewrite" url="images/002.jpg" appendQueryString="false" />
</rule>
</rules>

I tested your URL rewrite rule and it works normally on my side.
So please try to clear your browser’s cache and try again.
And do you get any errors? If not, you can only use FRT to view detailed messages.

Related

IIS UrlRewrite for reverse proxy only working if folder exists on primary website

I've got two websites on the same box and I want to reverse proxy a folder on one to the other. It is only working for directories if I create the directory on the parent website.
so i want http://site/odata/Books
to go to
http://myodatasite/odata/Books
Pretty straightforward, but it is not working unless I go to wwwroot/site and create the /odata/Books folders there. If I do this, the rewrite works. If I call /odata/Authors, again it will 404 unless I create an Authors folder.
It seems like I need to have a wilcard mapping in place in IIS or something else is causing the rewrite to fail. I have tried adding a wildcard mapping pointing to aspnet_isapi and had no changes, possibly because of integrated mode but I haven't found anything that is helpful online yet for this.
Does anyone know why my rewrite is not working for directories but a redirect works fine?
<rewrite>
<rules>
<!-- Does Not Work unless folders exist on current site! -->
<rule name="OdataRoutes" stopProcessing="true">
<match url="^odata/(.*)" />
<action type="Rewrite" url="http://myodatasite/odata/{R:1}" />
</rule>
<!-- Works -->
<rule name="OdataRoutes2" stopProcessing="true">
<match url="odata2/(.*)" />
<action type="Redirect" url="http://myodatasite/odata/{R:1}" />
</rule>
<!-- Works -->
<rule name="OdataRoutes3" stopProcessing="true">
<match url="(.*)\.odata" />
<action type="Rewrite" url="http://myodatasite/odata/{R:1}" />
</rule>
</rewrite>
Looks like it wasn't the mappings at all. Turning on Failed request tracing and monitoring the successful request and the failed request, it seemed the directory rewrite was being passed from ARR to the MvcHandler. Not sure why the redirect wasnt and the rewrite was, but after seeing that, the fix was pretty easy.
routes.IgnoreRoute("odata/{*pathInfo}");

Rewrite rule not working on IIS with using Regex

I'm trying to write a rewrite rule in on Windows Server 6.2. Although I used IIS Manager to create the code, it didn't work.
I tried stopProcess true/false, used different regex, restart server several times. Nothing changed. I followed the whole steps on Microsoft's web site on https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to e-campus" stopProcessing="true">
<match url="[^\/]+\/\/([^\/]+:?[0-9]?)\/.*" />
<action type="Rewrite" url="{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
I want to show only main URL. My domain is http://e-campus.example.com.
For example if someone go to that link: http://e-campus.example.com/Login/Student
Server should rewrite to this:
e-campus.example.com (with hiding http:// but it's not important)
So basically I just want to show main URL. But it keeps showing full path. What am I missing here?
According to your description, I found your regex match the whole url. But the iis url rewrite will not get the whole domain, it will just get the part of the url not the whole url.
For example:
If your url is http://e-campus.example.com/Login/Student., the match url part is
login/Student.
So if you want to rewrithe all the request to e-campus.example.com, you should use below url rewrite rule.
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite to e-campus" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://e-campus.example.com/" />
</rule>
</rules>
</rewrite>
</system.webServer>

IIS URL Redirect failing

I need to redirect URLs of the form:
http://server1.name.here/path1/path2/?num=123456
to:
http://server2.name.here/path3/path2/?num=123456
using the IIS URL Rewrite Module 2.0 on IIS 8.5. I've used a DNS alias to handle the server name redirection.
I've been using the user interface to configure the path rewrite but despite trying several variations and extensive research I cannot get my rewrite to work, which is slightly embarrassing as I feel this should be a simple rewrite. The web.config produced by the URL rewite user interface is:
<rewrite>
<rules>
<rule name="path rewrite" patternSyntax="ECMAScript" stopProcessing="false">
<match url="path1/path2/" ignoreCase="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="num=[0-9]+" />
</conditions>
<action type="Rewrite" url="path3/path2/" appendQueryString="true" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
When I try to open server1.name.here/path1/path2/?num=123456 I get a "403 - Forbidden: Access is denied." and the logs show no evidence that my URL is being rewritten or even flagged by the rewrite rule.
I'm sure I'm missing something obvious, can anyone enlighten me as to what I've got wrong?
Many Thanks Eden

Rewrite rules in ISS's web.config

I'm just trying to get some code running on ISS that has been running on Apache for a long time.
A particular mod_rewrite rule is proving difficult to get working.
In Apache I've used:
RewriteRule ^media/(js|css|img|font)/(.+)\.(\d+)\.(js|css|png|jpg|gif)$ /media/$1/$2.$4
[L]
To turn a URL like /media/css/style.1367406756.css into /media/css/style.css, letting me put timestamps in the files to avoid caching issues.
In my web.config file I've added:
<rule name="Cache bust assets" stopProcessing="true">
<match url="^(.*)$"/>
<conditions>
<add input="{URL}" pattern="^media/(js|css|img|font)/(.+)\.(\d+)\.(js|css|png|jpg|gif)$"/>
</conditions>
<action type="Rewrite" url="/media/{R:1}/{R:2}.{R:4}" appendQueryString="true" />
</rule>
Which, looks like it should work. (It's alongside another rule, which works fine, so it's not that the server isn't parsing it or anything).
I don't get any errors, other than visiting /media/css/style.1367406756.css gives me a 404 error.
How Can I make this work?
You use {R:N} back references in your action when you should use {C:N} since the information comes from the conditions. See Using Back-references in Rewrite Rules for more information.
You can see this error if you open the iismanager console:
To get your rule to work you have 2 solutions.
First you can change your action to (using {C:N}):
<action type="Rewrite" url="/media/{C:1}/{C:2}.{C:4}" appendQueryString="true" />
Or (and I do think it is a better solution), you can use the Import mod_rewrite Rules tool.
In the iismanager console, click on Import Rules... (on the right tab) and paste your apache rule:
This will create for you the following rule:
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^media/(js|css|img|font)/(.+)\.(\d+)\.(js|css|png|jpg|gif)$" ignoreCase="false" />
<action type="Rewrite" url="/media/{R:1}/{R:2}.{R:4}" />
</rule>

Redirect “/” to “/web” using IIS7

I'm hosting a site where I would like for various reasons to have http://mydomain.com automatically redirect to http://mydomain.com/web while at the same time still allowing http://mydomain.com/foo.html to be served.
Using HTTP Redirect from IIS 7 I seem to be creating an endless redirect loop. Would you have any hints for me?
Give URL Rewrite Module a try. Following code should work for you :
<rewrite>
<rules>
<rule name="Redirect example.com to example.comn/web" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/web" />
</rule>
</rules>
</rewrite>
How you can get start with Rewrite Module is briefly documented on below post :
http://www.tugberkugurlu.com/archive/remove-trailing-slash-from-the-urls-of-your-asp-net-web-site-with-iis-7-url-rewrite-module

Resources