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 4125 posts 22223 karma points MVP 9x c-trib
    Sep 26, 2012 @ 13:09
    Matt Brailsford
    0

    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"
        virtualUrl="^(/media/([0-9]+)/.*)$"
        rewriteUrlParameter="ExcludeFromClientQueryString"
        destinationUrl="/MediaHandler.ashx?id=$2&amp;url=$1"
        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
    0

    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 urlrewriter.net 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 urlrewriter.net handler won't be used for a simple rewrite

    Keep us posted.

    cheers,
    doug. 

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

    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...

    /Chriztian

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

    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:

    ^/media/([0-9]+)/.+$
    /MediaHandler.ashx?id={R:1}&url={R:0}

    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 4125 posts 22223 karma points MVP 9x c-trib
    Sep 26, 2012 @ 15:28
    Matt Brailsford
    0

    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.

    Matt

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

    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
    0

    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!

    cheers,
    doug. 

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

    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?

     

    Best,

    Sören

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

    Hello,

    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"
      virtualUrl="^(/media/([0-9]+)/.*\.(?!jpg|jpeg|png|gif|svg|tif)[^.]+)$"
      rewriteUrlParameter="ExcludeFromClientQueryString"
      destinationUrl="/FileHandler.ashx?url=$1"
      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" />
    </rule>
    

    ... 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
    Megha
    0

    Hello Martin

    using IIS Rewriting i want to rewrite media file for all content pages and rewrite as //cdn.mydomain.com... 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:

Draft