Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Matt Brailsford 4124 posts 22220 karma points MVP 9x c-trib
    Sep 26, 2012 @ 13:09
    Matt Brailsford

    URL rewriting media

    Hey Guys,

    I'm trying to setup a medi proxy to prevent people accessing files which have an expiry date on. To do this, I want to create a global media handler. So my idea was to add a URL rewrite rule to point all media to my handler as follows:

    <add name="media"
        ignoreCase="true" />

    However, this doesn't seem to work. It never gets hit. The weird thing is, if I convert this to a redirect instead of a rewrite by adding the redirect="Application" attribute to it, it fires.

    Anybody got any ideas why original rewrite isn't firing?

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Sep 26, 2012 @ 14:13
    Douglas Robar

    Okay, I'm speaking from ignorance because I haven't done this myself but I can imagine a couple things that might be relevant...

    1. Set the app pool to send all requests (or at least all media requests) through .net
    2. Try with the iis rewriter instead of that's in umbraco
    3. If #2 works you have a solution. If it doesn't, it isn't umbraco's fault, look elsewhere
    4. If #2 works and you want to look at it further, consider this question: is there any need for umbraco to handle this request at all since the /media folders are not 'virtual' at all. Thus it seems like umbraco's built-in handler won't be used for a simple rewrite

    Keep us posted.


  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Sep 26, 2012 @ 14:19
    Chriztian Steinmeier

    Hi Matt,

    I can never figure out which RegEx features are available for the UrlRewriter, i.e., normally you'd have to escape those forward-slashes... and I seem to remember not having to escape the dot in some cases, either - which would again mean that the dot you're using in the "normal" way (as anything but a newline) probably shouldn't work, no?

    Be good to know what the real problem is when you solve it...


  • Stefan Kip 1614 posts 4131 karma points c-trib
    Sep 26, 2012 @ 14:22
    Stefan Kip

    I always use the IIS UrlRewrite plugin, just because it's got more features and works all the time ;-)
    It is weird that the rule works when using a redirect instead of rewrite.

    Just wanted to mention that I'd use this instead with IIS UrlRewrite:


    Btw, I'm not sure what you're doing with the id, because it's not the media id (as you'll probably know...)

  • Matt Brailsford 4124 posts 22220 karma points MVP 9x c-trib
    Sep 26, 2012 @ 15:28
    Matt Brailsford

    Hey Guys,

    I couldn't figure out why it didn't work (did dougs suggestions, though couldn't get IIS rewrites working in IIS Express either), so ended up creating a HttpHandler against the path "/media/*" instead, this then triggers for every media request which I then just run the regex internally to parse the url.

    @kipusoep I parse the property id to do a lookup and fetch the media id. Doing it in the regex just means I wouldn't have had to parse it, instead it would just have been passed in on the querystring.

    Thanks for the suggestion though guys.


  • Stefan Kip 1614 posts 4131 karma points c-trib
    Sep 26, 2012 @ 15:47
    Stefan Kip

    Ah that's what I thought when typing my comment :-)

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Sep 26, 2012 @ 15:55
    Douglas Robar

    You need to install the iis rewrite package for IIS, it isn't installed by default in either IIS or IIS Express.

    But glad you got a solution in the end!


  • Sören Deger 733 posts 2844 karma points c-trib
    Feb 24, 2015 @ 20:49
    Sören Deger

    Hi guys,

    a long time has passed since this topic has been started.

    Is there a way to make this in Umbraco 7? Or is it still only using IIS? Are there any new insights?


    In my specific case, users want to be able to put a custom URL for each media node.

    Any ideas how I can solve this?




  • Martin Carlsén 5 posts 75 karma points
    Jun 11, 2016 @ 22:48
    Martin Carlsén


    I was stumble upon this thread today trying to accomplish almost the same thing: prevent people accessing files. I used the following URL rewrite to point all media, except images, to a generic handler:

    <add name="Files"
      ignoreCase="true" />

    But unless adding redirect="Application" attribute it didn´t work. So I installed the IIS Rewrite module and added the following rewrite rule in Web.config ...

    <rule name="File redirect" stopProcessing="true">
       <match url="^(media/([0-9]+)/.*\.(?!jpg|jpeg|png|gif|svg|tif)[^.]+)$" />
       <action type="Rewrite" url="/FileHandler.ashx?url={R:1}" appendQueryString="false" />

    ... which worked as expected.

    Unless I didn´t do it wrong, IIS is still the way to go. Someone else who got any insights to this?

    Update: Watching Shannon at the Umbraco UK festival 2015, talking about Umbraco V8 and beyond. The UrlRewriting.NET will not be shipped in Umbraco anymore due to bugs and inactivity. He also recommend IIS Rewriting. So I guess that ties it all together. :)

  • Megha 10 posts 80 karma points notactivated
    Feb 02, 2024 @ 12:30

    Hello Martin

    using IIS Rewriting i want to rewrite media file for all content pages and rewrite as // because these images get from cdn ..i try your pattern but its not work please help me to do so

Please Sign in or register to post replies

Write your reply to: