Copied to clipboard

Flag this post as spam?

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

  • trfletch 569 posts 544 karma points
    1 week ago

    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" />

    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 569 posts 544 karma points
    5 days ago


  • Dan Diplo 1204 posts 4275 karma points
    5 days ago
    Dan Diplo

    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 569 posts 544 karma points
    4 days ago

    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:


    To this:


    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):

Please Sign in or register to post replies

Write your reply to: