Rewrite images directory IIS - asp.net-mvc

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>

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>

Remove Subdomain from URL in IIS or ASP.net MVC or haproxy

Is it possible to remove a subdomain (or basically treat it like www) from the URL?
Example:
subdomain.domain.com/specific/link/forsubdomain -> domain.com/specific/link/forsubdomain
and not have it point to the primary domain and return a 404 error,
example:
domain.com/specific/link/forsubdomain -> return a 404 because it only exists in the subdomain.
If it's possible to do something in Haproxy as well Or disguising URLs in ASP.net MVC Route table modifications im open to it.
Not just IIS configuration.
Just wanted to know if it's possible to change the URL as i described and still have it point to the subdomain site.
If I understand your question correctly you are refering to canonical URLs.
Within IIS you can use URL Rewrite, which is a module you can install manually or via the Web Platform Installer (if it's not already installed).
This allows you to create a url rewrite rule which you can configure per site, something along the lines of;
<rule name=”Redirect URL to WWW version”
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>
You can add the rules manually to the site's web.config file or use a GUI within the IIS site manager tool.
There's a lot of reference and a number of tutorials available from Microsoft and numerous bloggers.
An example of a complete web.config just doing this type of redirect.
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="WWW Redirect" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^contoso.com$" />
</conditions>
<action type="Redirect" url="http://www.contoso.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Using URL rewrite with sub domains; download file fails

I currently have this rewrite URL rule in my web.config file
<rule name="Rewrite to qa" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^qa.golfgameskeeper.com$" />
</conditions>
<action type="Rewrite" url="qa/{R:1}" />
</rule>
it works great, makes qa.golfgameskeeper.com/qa work like qa.golfgameskeeper.com
However, when I try to download a file from a sub-directory of qa it seems to get confused
http://qa.golfgameskeeper.com/apps/iOS will list the file, but will not allow me to download it.
Even when clicking one the link above the rule re-writes the link to
qa.golfgameskeeper.com/qa/apps/iOS (cut and paste will work, not clicking the link)
Is there a way to modify this rule to allow what I'm trying to do? So, as I'm writing this I figured out what I am trying to do.
have
qa.golfgameskeeper.com -> qa.golfgameskeeper.com/qa (works)
and allow
qa.golfgameskeeper.com/apps/iOS to download the file without rewriting the URL twice (which is what I think it does).
Thank you,
You can add a rule before your rewrite rule to match on the apps subdirectory and do nothing, basically skipping the rewrite rule.
<rule name="Skip apps" stopProcessing="true">
<match url="^apps/(.*)" />
<action type="None" />
</rule>
<rule>
//your rewrite rule here
</rule>

Route static files to new path in asp.net mvc

I have 1000 static html page files and html data in Database.
Now i want to move to amazon s3 server, i will move all of my static files to s3.
How i can route the path to the new location, i can't update all of the pages and correct images path.
like:
<img src='/myfiles/images/blog/20140606/flow.jpg'/>
to
Request: /myfiles/images/blog/20140606/flow.jpg
Redirect to: htps://xxx.s3.amazonaws.com/myfiles/images/blog/20140606/flow.jpg
i need a route to redirect the request to new location instead of update all files.
May be using a url rewrite in your web.config like this:
<rewrite>
<rules>
<rule name="Rewrite to s3" stopProcessing="true">
<match url="^/myfiles/images/blog/(.*)" />
<action type="Rewrite" url="s3/mybucketname/{R:1}" appendQueryString="false" redirectType="Found" />
</rule>
</rules>
</rewrite>

ASP.NET MVC 4 public folder like in Ruby on Rails

Is there a way to give an ASP.NET MVC 4 project a "public" folder, which would act like the application root, like in Ruby on Rails?
ASP.NET MVC usually comes with a "Content" folder where you can put statically served files like style sheets, scripts, and images. But the URLs for these files have to include the Content folder, for example <img src="/Content/logo.png">.Is it possible to make the Content folder the root of the application so you can use this instead: <img src="/logo.png">?
I'm not sure why I didn't think of this before, but I was able to accomplish this with IIS URL Rewrite. The following rules assume a folder named "Public" where your static files reside. Of course, you can name the folder whatever you want.
<!-- Any direct references to files in the Public folder should be
301 redirected to maintain canonical URLs. This is optional. -->
<rule name="Public folder canonical path" stopProcessing="true">
<match url="^public/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{R:1}" appendQueryString="true" />
</rule>
<!-- Any URL that points to an existing file after prepending "Public" to it
will serve that file. For example, if a file exists at /Public/style.css
then the URL /style.css will serve that file. Likewise, if a file exists
at /Public/images/logo.png, then the URL /images/logo.png will serve
that file. Files in the Public folder will take precedence over files in
the application root, so if a file /Public/script.js exists and a file
/script.js exists, only the /Public/script.js file will be served. This
also takes precedence over MVC routes. -->
<rule name="Public folder" stopProcessing="true">
<match url=".+" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="^/Public/" negate="true" />
<add input="{DOCUMENT_ROOT}/Public{URL}" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="Public/{R:0}" appendQueryString="true" />
</rule>

Resources