Custom Path with ImageResizer SqlReader - asp.net-mvc

I am developing an application that will have friendly urls to dealer logo images and I would like to use the SqlReader Plugin for ImageResizer.
In the examples I read on their website, you can easily host files from SQL all using the same paths (e.g. ~/databaseimages/{id}) but I'd really like to be able to do something to the effect of ~/michigan/dealers/{id}/logo.jpg where michigan could potentially be any state name.
Is it possible to have a multi-part id (state & id) and have the filename be static as I've shown, or is there a better way to do this?! I really would prefer not to have to change the url if I don't have to.
Solution
Based on the accepted answer, this is the URL Rewrite solution I used
<rewrite>
<rules>
<rule name="Rewrite to dealerlogos SqlReader" stopProcessing="true">
<match url="^([_0-9a-z-]+)/dealers/([_0-9a-z-]+)/logo.jpg" />
<action type="Rewrite" url="dealerlogos/{R:2}?regionSlug={R:1}" appendQueryString="true" redirectType="Found" />
</rule>
</rules>
</rewrite>

You can do this with URL rewriting of any kind.
ImageResizer provides a built-in event ImageResizer.Configuration.Config.Current.Pipeline.Rewrite that you can attach a handler to if you want to rewrite the path only for ImageResizer requests.

Related

IIS URL Rewrite is not working when redirecting to another server

I need to redirect links to a different server using URL rewrite in IIS. These are SRSS report links. I have tried so many different configurations i have given up.
Old Link:
http://OldServer/ReportServer/Pages/ReportViewer.aspx?(reportnamehere - need to copy this part across to new link)
http://NewServer/ReportServer/Pages/ReportViewer.aspx?reportnamehere
Any ideas how this can be achieved?
This is a very simple rule requirement. Something like this will work. Basically just redirects to a new server appending the entire URL
<rule name="Redirect to newServer" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://NewServer/{R:1}" redirectType="Temporary" />
</rule>

IIS / MVC web.config rewrite

I have two projects on my server on running at same ip.
a.company.com (MVC)
b.company.com (WEBAPI)
I want to redirect;
a.company.com/api/(.*) => **b.company.com/(.*)
Like that, but i want to keep b.company.com hostname is hidden. Its mean i want to rewrite it, not redirect.
Thanks.
You will need to have the URL Rewrite module 2.0 loaded, which is available via the platform installer. this will allow you to do server-side URL rewriting.
I can't say exactly what your rules should be but if you do it at the site level you will get somthing like this in your web.config in the system.webServer section:
<rewrite>
<rules>
<rule name="rewrite" patternSyntax="Wildcard">
<match url="/api/*" />
<action type="Rewrite" url="/{R:1}" />
</rule>
</rules>
</rewrite>
You will need to use the correct capture groups to match your situation.

IIS 7.5 URL Rewrite: Is there a better way to rewrite / combine multiple rules

We are building a new mobile based website for an already existing website (that is used heavily by the client).
In this scenario when a user requests certain webpages on the existing web-application from a mobile device, the request for the existing web application must be Redirected to the new mobile web application.
To summarize, we have the following conditions-
If the web page request contains a Query String (jobId), it must be
redirected to a mobile web page (JobDtls.aspx) used with another
Query String parameter name (jId); but with the same query string
value.
If the web page request does not contain a query string, the
Redirection must be to the default.aspx page of the mobile web site.
Both the above conditions must work only if the request is through a
mobile device.
For this Task, I came up with 2 different rewrite rules as described below. However, since I am new to this topic, I wanted to know if someone could optimize on this configuration; with maybe 1 Rule instead.
<rewrite>
<rules>
<rule name="Mobile Entry With QueryString" stopProcessing="true">
<match url="Job.aspx" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_USER_AGENT}" pattern="midp|mobile|phone" />
<add input="{QUERY_STRING}" pattern="jobid=([0-9]+)$" />
</conditions>
<action type="Redirect" url="htps://{HTTP_HOST}/MWeb/mjobitem.aspx?jid={C:1}" rdirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="Mobile Entry Without QueryString" stopProcessing="true">
<match url="Job.aspx" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_USER_AGENT}" pattern="midp|mobile|phone" />
<add input="{QUERY_STRING}" pattern=".+" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/MWeb" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
First, it does not looks like your case may benefit of any form of combination of your rules.
"Combining" redirect rules makes especially sense when you want to avoid redirects chaining. Your case is not leading to redirects chaining. See Combine Multiple IIS7 Rewrite Rules (Redirects) Into One for more on this (and especialy answer link, if the answer is still not completed with the solution provided by the link). Moreover, it is not really combining rules in one, it does allow them to run and accumulates prior to performing the final resulting action.
Then, it looks like we can not have in a same rule some conditions with logicalGrouping MatchAll and others with logicalGrouping MatchAny, which would be required for combining conditions of both rules. An other way would be to elaborate a more complex regular expression matching both query string condition. But it would lead to something harder to understand, so this looks to me undesirable.
And this is just for combining conditions. You would have another trouble to tackle: actions are not the same. I do not think there is a way there to express an action handling both of your cases.

Force a language prefix in the url with Sitecore

Is there a setting to force the language in the URL? Like, if I browse to http://www.site.com, I should be redirected to http://www.site.com/en, as it is now I can see the start page without the language prefix.
The LinkManager is configured to always put in the prefix so all the links look fine at least.
Another way to use is our SEO Friendly URL Module.
This module implements a custom LinkProvider that provides SEO Friendly URL's and forces items to be accessed through their friendly URL.
So if an item is accessed without the language code in the URL (e.g. /my-item) the module will 301 redirect to the URL with language code (e.g. /en/my-item).
That is, if you have configured it to force friendly URL's (forceFriendlyUrl="true") and set languageEmbedding="always".
We use that module on our corporate website, so take a look at that to see it in action.
You can use ISS URL Rewrite to redirect / to /en
Install that module on the IIS, and setup a rule.
It can be done from the IIS gui or from web.config
I am using this rule to do the same.
Insert the following configuration in the system.webServer section in web.config
<system.webServer>
<rewrite>
<rule name="redirert / to en" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{PATH_INFO}" pattern="^/$" />
</conditions>
<action type="Redirect" url="/en/" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
It will redirect every request hitten "/" to "/en"

Rewrite images directory IIS

We are migrating a legacy application to ASP.NET MVC, but in the old application the images folder was in the root of the app, so to reference an images the path would be /images/imagename.png, but we want to move the images in to /Content/images in the new application but we dont want to change the paths in all the css and the html.
Is there a way to do a rewrite to the images folder so that every URL that goes to /images goes to /Content/images instead?
Use URL Rewrite. You need to install it via WebPI, then add the following to your web.config in system.webServer:
<rewrite>
<rule name="Old Images" stopProcessing="true">
<match url="^images/(.+)$" ignoreCase="true" />
<action type="Rewrite" url="content/images/{R:1}" appendQueryString="true" />
</rule>
</rewrite>

Resources