Forwarding requests for relocated static content
-
14-11-2019 - |
Question
I have a bunch of static content on a site that has always lived in the root directory like http://mysite.com/smiley.gif
. I want to move it all to a subdirectory http://mysite.com/images/smiley.gif
.
The problem is that years of content points to the first URL. What's the best way to forward those requests to the new locations?
Solution
We've recently done exactly this. We ended up using rewrite rules though IIS because they are applied very early in the pipeline and so was the smallest performance impact. Take a look at the UrlRewrite module for more info.
Just got our rules open, after installing the rewrite module in IIS, you add the following to your root Web.config. This rule would rewrite all *.gif requests, you might have to tailor it a bit.
<system.webserver>
<rewrite>
<rules>
<clear />
<rule name="gif" stopProcessing="true">
<match url="^(.*).gif" />
<action type="Rewrite" url="/images/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webserver>