Copied to clipboard

Flag this post as spam?

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


  • trfletch 598 posts 604 karma points
    Feb 07, 2018 @ 13:32
    trfletch
    0

    URL rewrite rule causing problems with @URL.Action and also URL's that start with a tilde ~

    I have an Umbraco 7 MVC website and I have added the following rewrite rule into the web.config:

          <rule name="Book rewrite">
          <match url="^books/(.*)/(.*)" />
          <action type="Rewrite" url="/product-display?isbn={R:1}" appendQueryString="false" />
        </rule>
    

    The rewrite rule works, however it causes problems with any ajax calls or anything that uses @Url.Action

    If for example I was on the following page: "/books/9780334042013/alive-to-the-word"

    Then any @Url.Action that was used on the page would point to "/books/9780334042013/umbraco/Surface............" instead of pointing to "umbraco/Surface.........."

    Also anything that has a tilde at the start will also point to the wrong URL, for example the following:

    <img src="~/Content/images/logo.png" />
    

    Would point to:

    <img src="/books/9780334042013/Content/images/logo.png"  />
    

    Does anyone know how I can resolve these issues so that I can get my URL rewrite rules to work correctly?

  • trfletch 598 posts 604 karma points
    Feb 14, 2018 @ 12:23
    trfletch
    0

    Bump

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Feb 14, 2018 @ 12:48
    Dan Diplo
    0

    The issue is that your match expression will basically match any URL that starts with books followed by two forward-slashes.

    You could tighten it up a bit to match books followed by a number and then a title so it becomes:

    <match url="^books/(\d+)/(.+)" />
    

    (The \d+ matches one or more digits and the .+ matches at least one character.

    If you can I'd also add an extra segment into your original URL that makes it more unique:

     <match url="^books/book/(\d+)/(.+)" />
    

    That way it would only match URLs of form /books/book/9780334042013/title/

  • trfletch 598 posts 604 karma points
    Feb 15, 2018 @ 14:30
    trfletch
    0

    Hi Dan,

    Thank you for the response but I do not think that is the case here, the rewrite rule is being applied to things that do not even contains books in the URL for example it is changing what should be this:

    /umbraco/Surface/Basket/AddItemToBasket?productId=1234&isEbook=true&cartId=12345&quantity=1
    

    To this:

     /books/9780334042013/umbraco/Surface/Basket/AddItemToBasket?productId=1234&isEbook=true&cartId=12345&quantity=1
    

    This is only the case when using @Url.Action, if I was to manually type the URL above it would not rewrite it (unless I added a tilde at the start).

    Also I would not be able to apply your rules because the number part can sometimes contain other characters and the title could effectively just be a number.

    I cannot change the URL's either (not without having to setup a lot of 301 redirects) because this is actually an upgrade of a current Umbraco site that has been live for years. The rewrite rules have always worked correctly on the current site using the UrlRewriting.config file but this functionality has now been removed, plus as mentioned above it seems to only be when using @Url.Action in MVC (the old site didn't use MVC).

    This article on stack overflow appears to explain the issue and they suggest using Routing instead of rewrite rules, however I am not sure this is possible in Umbraco because I do not want to route to a controller and call an action because I need to open the actual Umbraco node if that makes sense): https://stackoverflow.com/questions/5801285/incorrect-url-action-when-url-rewrite-is-used

  • trfletch 598 posts 604 karma points
    Feb 21, 2018 @ 10:44
    trfletch
    0

    Bump again

  • trfletch 598 posts 604 karma points
    Mar 02, 2018 @ 15:51
    trfletch
    0

    Surely someone else has come across this or do people not URL rewrite rules in Umbraco anymore?

  • Tim Mather 33 posts 136 karma points
    Mar 23, 2018 @ 14:09
    Tim Mather
    0

    We are having the same problem, it is happening in a master view where we are referencing scripts, I am assuming it is something to do with rewrites as it doesn't happen when I disable all rewrites.

  • Tim Mather 33 posts 136 karma points
    Mar 23, 2018 @ 15:29
  • trfletch 598 posts 604 karma points
    May 16, 2018 @ 17:09
    trfletch
    0

    Hi Tim,

    Did you ever find a solution for this? I am back working on the websites that have this issue and need to resolve the problem ASAP because they will be going live soon.

    I would rather not have to replace all instances of @Url.Action with the actual URL (without a tilde of course) but it seems I may not have any other option at this point if I want to get my rewrite rules working.

  • Tim Mather 33 posts 136 karma points
    May 16, 2018 @ 17:27
    Tim Mather
    0

    Yes this works for a number of our projects.

    https://support.microsoft.com/en-nz/help/2905164/tilde-notation-maps-to-the-original-urls-by-using-iis-url-rewrite-in-a

    Seems crazy you have to do this though. You will need to sort out the Umbraco global.asax but easy done just search google and should come up.

Please Sign in or register to post replies

Write your reply to:

Draft