Working with media in Umbraco with multiple environments

Written by Anders Bjerner on Thursday, December 19, 2013 at 1:36 PM GMT
Umbraco
UrlRewriting
Media

At Skybrud.dk where I work, we have established an Umbraco setup with three steps of deployment - a local developer environment for each developer, a stage environment and finally a live environment.

Working with media across these environments can be a nightmare, since you might have to copy files back and forth between the three environments. However with a very small amount of URL rewriting, nightmares can be turned into pure joy.

To make this work, we should set the basic rule that all new media are uploaded to a single of these environments. If the site has been launched, it will make most sense to use the live environment.

On the technical part, Umbraco ships with a package called Urlrewriting.Net, which let us setup redirect rules in /config/umbracoSettings.config by using XML and regular expressions. So if we assume we're using the domains dev.mysite.com, stage.mysite.com and www.mysite.com, you should add something like this to the /config/umbracoSettings.config file:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
<add name="MediaDeveloper"
     virtualUrl="^http\://dev\.mysite\.com/(media|cropup)/(.*)"
     rewriteUrlParameter="ExcludeFromClientQueryString"
     destinationUrl="http://www.mysite.com/$1/$2"
     redirect="Domain"
     ignoreCase="true" />

                                    
<add name="MediaStage"
     virtualUrl="^http\://stage\.mysite\.com/(media|cropup)/(.*)"
     rewriteUrlParameter="ExcludeFromClientQueryString"
     destinationUrl="http://www.mysite.com/$1/$2"
     redirect="Domain"
     ignoreCase="true" />

That's it! When trying to access a file in your media folder in either your dev or stage envorionment, you will now automatically be redirected to the live server. Rules in Urlrewriting.Net are created using using regular expressions, and the rules above is constructed to support URLs starting with /media/ as well as URLs starting with /cropup/.

If you're using ImageGen instead of CropUp, your rewrite rules should look something like the ones below:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
<add name="ImageGenDeveloper"
	 virtualUrl="^http\://dev\.mysite\.com/ImageGen\.ashx"
	 rewriteUrlParameter="ExcludeFromClientQueryString"
	 destinationUrl="http://www.mysite.com/ImageGen.ashx"
	 redirect="Domain"
	 ignoreCase="true" />

                                    
<add name="ImageGenStage"
	 virtualUrl="^http\://stage\.mysite\.com/ImageGen\.ashx"
	 rewriteUrlParameter="ExcludeFromClientQueryString"
	 destinationUrl="http://www.mysite.com/ImageGen.ashx"
	 redirect="Domain"
	 ignoreCase="true" />