IIS7 Rewrite Image Files - iis-7

I'm wondering if it is possible to have an IIS7 URL rewrite rule that does something like this. When someone goes to:
http://www.mydomain.com/images/0000022_0-Hello_World.jpeg
it will request the file at:
http://www.mydomain.com/images/0000022_0.jpeg
I tried to make a rule to match (.)/images/(.)-(.*).jpeg and then rewrite it to {R:1}/images/{R:2}.jpeg, but it doesn't work. I just get a 404 error. I also tried adding the ISAPI Wildcard, but it didn't change anything.
I'm using .net 4.0
Thanks!

Assuming you use IIS Manager ( like in this article):
Try to create a rewrite rule which matches the following pattern (regular expressions):
^images/([0-9_]+)-.*\.jpeg$
As Action, choose rewrite, and fill in the following rewrite Url:
images/{R:1}.jpeg
No other rule should be executed after that (-> Stop processing of subsequent rules checkbox)

You don't need to add the ISAPI Wildcard for this as you have an extension (.jpeg) so should be handled by the StaticFile Handler.

You should put this after other rewrite rules ,for virtual folders etc..
<rule name="rule 1s" stopProcessing="true">
<match url="\.(gif|jpe?g|png|bmp)" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="//ressources/default.png" />
</rule>

Related

How to remove querystring when using Umbraco rewrite

I have a rewrite rule as below to my Umbraco/ASP .Net site
<add name="Test" virtualUrl="^~/category/(.*)" destinationUrl="/category?cat=$1" rewriteUrlParameter="ExcludeFromClientQueryString" ignoreCase="true" />
The idea is if a user types
www.example.com/category/electronics
www.example.com/category/devices
www.example.com/category/food
www.example.com/category/beverages
Everything works as i would expect when they reach the page without any rules and plain URLs i.e.
www.example.com/category?cat=electronics
If i have the above rule it enabled it always adds/forwards the querystring parameter i.e cat= when you click a link on any of the pages above.
How could i stop forwarding/adding the parameter?
You could use below URL rewrite rule:
<rule name="add query string" stopProcessing="true">
<match url="^category/([^/]+)/$" />
<action type="Redirect" url="http://www.sample1.com/category?cat={R:1}" appendQueryString="false" />
</rule>
if you did not install URL rite module in iis you could install it from the below link:
https://www.iis.net/downloads/microsoft/url-rewrite

Rewrite Rule: Question about regex pattern

I can't figure out why a certain rewrite rule does not work.
I have this rule:
<rule name="CanonicalHostNameRule1" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="testsite\.com" negate="true" />
</conditions>
<action type="Redirect" url="https://www.testsite.com/{R:1}" />
</rule>
When I go into IIS and using the URL Rewrite module, I tested using "http://testsite.com". This works. But after placing an updated web.config in production, these are my results from typing in these into the url:
I was able to reach the site by typing in these variations:
https://www.testsite.com
http://www.testsite.com
www.testsite.com
testsite.com
But I received the error message "testsite.com refused to connect" when typing in:
https://testsite.com
http://testsite.com
What am I missing, that is preventing the last 2 from working? It seems like the rewrite rule I have should catch all requests.
Thank you in advance!
My guess would be that for the last two examples, their {HTTP_HOST} will have no www which means that it will match your condition resulting in your negate attribute skipping your rule action.
I would recommend enabling IIS Failed Request Tracing in order to identify exactly why your rewrite rule isn't working for those values.

IIS URL rewrite module url's to lowercase

For better SEO we are using URL rewrite to convert all the URL's to lowercase. I set this one as mentioned in this the below article.
Everything is working fine from URL perspective, but we see lot of 301 redirects when we check in fiddler.
It looks like the images, javascript, css, jquery ajax calls and everything is getting converted into lower case.
I am trying to remove that and want to rewrite only aspx extension and no extension urls. I tried to play around the matchurl without any success. Any help or guidelines will be highly appricated.
Thanks
Edit:
My Current rule is
<rules>
<rulename="LowerCaseRule1"patternSyntax="ExactMatch"stopProcessing="true">
<matchurl="[A-Z]"ignoreCase="false"/>
<actiontype="Redirect"url="{ToLower:{URL}}"/>
</rule>
</rules>
You could probably use something as follow:
<rule name="LowerCaseRule1" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" pattern="\.aspx$" />
<add input="{REQUEST_FILENAME}" pattern="\." negate="true" />
</conditions>
</rule>
The rule will be triggered only if one of the condition is true:
The first one checks if the requested path (filename) ends with .aspx.
The second one checks if the if the requested path (filename) doesn't contain a . (so doesn't have an extension)

IIS Rewrite not working (but redirection does)

I was trying to play with URL re-writing using the Rewrite Module 2.0 but I had no luck getting it to work. What I'm trying to do is re-write all calls to web app at port 80 to other applications hosted in IIS (or maybe on different servers on the network). Using the GUI provided by IIS I created the following rule:
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="site1/(.*)" />
<action type="Rewrite" url="http://localhost:7001/{R:1}" />
</rule>
</rules>
</rewrite>
Quiet simple, but unfortunately it does not work. On the other hand, when I change the action type to Redirect, it works fine.
What could be the problem?
I ran into this same issue yesterday, and it took me a long time to figure out.
The key here is that you've got an http:// prefix in your rewrite action; that makes this a special case that needs to be handled by Application Request Routing. The first step is to make sure that the Application Request Routing module is installed. You can find the module at https://www.iis.net/downloads/microsoft/application-request-routing. Once that is installed, go to your IIS web server (a level up from your web site), and open the Application Request Routing Cache feature. From the actions on the right, choose Server.Proxy.Settings, and make sure that the "Enable Proxy" checkbox is checked. This allows the URL rewrite task to be re-routed to Application Request Routing, and your reverse proxy should work for external requests.
The idea came from this excellent blog post from 2009: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/
Stumbled across this old post when I was trying to solve the same issue.
SOLVED!
Using Rewrite URL feature in IIS Services Manager I created a friendly URL rule.
This worked ok and when I looked at the rule in the web.config file (www root) it showed 1 rule to redirect and 1 rule to rewrite.
I edited this to suit 1 match. Then I just duplicated this code editing the product ID for each. Example below:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^product\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^id_product=\b35\b" />
</conditions>
<action type="Redirect" url="990mm-bohemia-cast-iron-electric-radiator"
appendQueryString="false" />
</rule>
The first rule looks for the string "product.php" in the URL and "id_product=35", it then redirects to "990mm-bohemia-cast-iron-electric-radiator" which currently does not exist. Then (see below)
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^\b990mm-bohemia-cast-iron-electric-radiator\b" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="product.php?id_product=35" />
</rule>
This rule rewrites the "product.php?id_product=35" bit to `990mm-bohemia-cast-iron-electric-radiator", creating the new location for the redirect.
Do make sure MVC routing doesn't steal your request. To prevent that from happening, ignore the route you're trying to rewrite:
RouteTable.Routes.Ignore("blog/{*pathInfo}");
Inspired by: https://sitecore.stackexchange.com/questions/3645/how-to-setup-a-reverse-proxy-with-sitecore
Change the Rewrite URL to AbsolutePath instead putting http://...
it should be
<action type="Rewrite" url="{R:1}" />
It worked for me, but in my case, I have been rewrite to a fixed webpage.

IIS7 Url Rewrite - Why does Redirect work and Rewrite does not?

What I want to do is rewrite subdomains to the main application, and append the specified subdomain onto the query string. For instance, "http://a.main.com" should rewrite to "http://www.main.com/default.aspx?SD=a".
Here is my rewrite rule:
<rule name="SubDomain" stopProcessing="true">
<match url="^$" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^([A-Za-z0-9]+)\.main\.com$" />
</conditions>
<action type="Rewrite" url="http://www.main.com/default.aspx?SD={C:1}" logRewrittenUrl="false" />
</rule>
When I navigate my browser to "http://a.main.com", I get a 404. However, when I change the rule to be a redirect rule instead, it redirects correctly. The fact that it works when set to redirect mode, but not when set to rewrite mode confuses me greatly. What's going on?
FYI my HOSTS file is set up so that www.main.com and a.main.com both point to 127.0.0.1. The web site's only binding in IIS7 has its Host Name property set to 127.0.0.1.
The "http://www.main.com/" portion of the node's url property needed to be removed. Here's what it looks like now:
<action type="Rewrite" url="default.aspx?SD={C:1}" logRewrittenUrl="false" />
This works.

Resources