Simplifying Solutions
I develop locally on an app that has millions of “library” files that have been uploaded by users over the years. The image library is approaching 1TB in size, so obviously, I cannot be expected to sync or copy down that data for use locally. When any files are not local, then you do not get the true experience your users will.
I don’t know why I never though about this solution before but over on Lullabot, Sean Lange explained how he solved this issue using rewrite rules in Apache.
I use IIS so I needed something similar. IIS uses a web.config file so all I had to do was convert his logic into something IIS could use. The rewrite rule will inspect all outbound HTML and rewrite the src attribute on any img tag with a path of ‘/photos/’ for me. Obviously, I don’t want this rule to fire on the production server and since I do check-in my web.config file into SVN, I needed a precondition check so production does not process this rule too:
Under the <rules> block in web.config:1 2 3 4 5 6 7 8 9 10 11 12 | <outboundRules> <rule name="dev - rewrite SRC of library files to come from production" preCondition="IsHTML" stopProcessing="true"> <match filterByTags="Img" pattern="^/photos/(.*)" /> <action type="Rewrite" value="http://production.domain.com{R:0}" /> </rule> <preConditions> <preCondition name="IsHTML"> <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" /> <add input="{HTTP_HOST}" pattern="devdomain|localhost" /> </preCondition> </preConditions> </outboundRules> |
Line 3: only match on IMG files in the path that begins with /photos/
Line 4: prepend the match ({R:0}) with the production domain
Line 8: only inspect HTML as it is leaving the server – no need to check images or JS
Line 9: only perform this if the webserver is serving for the ‘devdomain’ or ‘localhost’ domains. Since my local dev environment will answer for either devdomain or localhost, I check the {HTTP_HOST} variable to detect if this a locally running version of the app – if not, then the app is on production.
Setting the rule’s preCondition="IsHTML" ensures it will only alter image files on production.
One caveat: you IIS can’t process outbound rules if compression (gzip) is active. I turn that off for development anyway.
Comments Closed.