Copied to clipboard

Flag this post as spam?

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


  • pbl_dk 150 posts 551 karma points
    Feb 20, 2018 @ 15:42
    pbl_dk
    0

    Query string to URL structure

    Hi there guys.

    I was hoping, someone could give me a hint or clue on how to solve a problem.

    In Umbraco I have a custom controller, where I query the controller with:

    /sys?country=denmark&city=viborg&occupation=webdevelopers
    

    Now the client wants the query to be a custom URL. So I have to get the query and return a URL which should be like below. And this URL should also return the original Query to the system when hit from Google.

    sys/denmark/viborg/webdevelopers
    

    Obviously there is no content page which match the url, its a fake URL based on querystrings.

    Any way to do this i Umbraco? I think it would be possible with general Routing in general ASP MVC.

    /Cheers Peter

  • Lewis Smith 211 posts 620 karma points c-trib
    Feb 20, 2018 @ 15:56
    Lewis Smith
    1

    You can set up a custom route, in the App_Start is where all your routing takes place.

    The way I handled this was using javascript to add to the URL but not refresh the page, then a controller that got the results and then parsed this in JSON.

    Then on page load (incase the user landed on the page with teh URL already changed to ys/denmark/viborg/webdevelopers then run this a return the results automatically!

    Thanks, Lewis

  • pbl_dk 150 posts 551 karma points
    Feb 20, 2018 @ 16:41
    pbl_dk
    0

    Hi Lewis,

    Thats a very good suggestion. It would work. I also found some documentation on URLrewrite which I believe is supported in Umbraco.

    https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

    Thanks for pointing me in the right direction.

  • Amir Khan 1287 posts 2744 karma points
    Dec 18, 2018 @ 17:04
    Amir Khan
    0

    Did you ever find a rewrite rule that actually does this? Most of what's out there seems to do it the other way around.

  • pbl_dk 150 posts 551 karma points
    Dec 18, 2018 @ 18:54
    pbl_dk
    0

    Hi Amir,

    Yes, it was quite easy to do with the IIS URL rewrite module wizard (SEO friendly URL).

    I.e. this snippet redirects /database?type=booktags to /database/booktags and Umbraco still works without a problem.

      <rewrite>
          <rules>
          ....
    
    
      <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
              <match url="^database$" />
              <conditions>
                <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                <add input="{QUERY_STRING}" pattern="^([^=&amp;]+)=([^=&amp;]+)$" />
              </conditions>
              <action type="Redirect" url="database/{C:1}/{C:2}" appendQueryString="false" />
            </rule>
            <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
              <match url="^database/([^/]+)/([^/]+)/?$" />
              <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
              </conditions>
              <action type="Rewrite" url="database?{R:1}={R:2}" />
            </rule>
    
  • Amir Khan 1287 posts 2744 karma points
    Dec 18, 2018 @ 20:19
    Amir Khan
    0

    Interesting, do you know how you might do it if /database was a variable portion of the url like the name of a location?

  • Amir Khan 1287 posts 2744 karma points
    Dec 18, 2018 @ 21:39
    Amir Khan
    0

    For example, this works perfectly when the name of location here portion is hard coded but I'm struggling without to pass that as a match anything into the action.

        <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
        <match url="^locations/name-of-location-here/$" />
        <conditions>
            <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
            <add input="{QUERY_STRING}" pattern="^contentIdString=([^=&amp;]+)&amp;contentNameString=([^=&amp;]+)$" />
        </conditions>
        <action type="Redirect" url="locations/name-of-location-here/{C:1}/{C:2}" appendQueryString="false" />
    </rule>
    <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
        <match url="^locations/name-of-location-here/([^/]+)/([^/]+)/?$" />
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="locations/name-of-location-here/?contentIdString={R:1}&amp;contentNameString={R:2}" />
    </rule>
    
  • pbl_dk 150 posts 551 karma points
    Dec 19, 2018 @ 07:09
    pbl_dk
    0

    I am not 100% sure, but I think that

    ([^/]+)/
    

    Means "anything". Those IIS redirects can be really hard to figure out.

    So maybe:

    ^locations/([^/]+)/([^/]+)/([^/]+)/?$
    

    I can see the problem, cause you need to assign the /name-of-location, somehow to a variable.

    I would go to https://forums.iis.net/ and ask them there.

  • Søren Kottal 713 posts 4571 karma points MVP 6x c-trib
    Dec 20, 2018 @ 20:35
    Søren Kottal
    0

    Hi Peter

    Although IIS Rewrite rules does the job, I would recommend you to take a look at custom MVC routes defined in Umbraco instead: https://our.umbraco.com/Documentation/Reference/Routing/custom-routes

  • pbl_dk 150 posts 551 karma points
    Dec 21, 2018 @ 13:00
    pbl_dk
    0

    Hi Søren

    Ok, I will give it a try this weekend. :-)

    Cheers. /Peter

  • Amir Khan 1287 posts 2744 karma points
    Jan 04, 2019 @ 19:37
    Amir Khan
    0

    Hi Søren, why is custom routes preferred?

  • pavel ringelhan 7 posts 78 karma points
    Jan 06, 2019 @ 15:48
    pavel ringelhan
    0

    Hi I have a similar problem. I'm trying to use iis url rewrite to overwrite the following address: ..../pobocky?filter=test on .../pobocky/test I have a rule set:

    <rewrite>
            <rules>
                <rule name="pobocky" stopProcessing="false">
                    <match url="^/pobocky/([_0-9a-z-]+)" negate="false" />
                    <action type="Rewrite" url="/pobocky?filter={R:1}" appendQueryString="true" logRewrittenUrl="true" />
                </rule>
            </rules>
    

    but I still get 404. What am I doing wrong Thanks

  • Amir Khan 1287 posts 2744 karma points
    Jan 07, 2019 @ 19:30
    Amir Khan
    0

    Hi Pavel, something like this should work for your needs. Its the variation in location name that's giving us issues.

    <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
    <match url="^pobocky/$" />
    <conditions>
        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
        <add input="{QUERY_STRING}" pattern="^filter=([^=&amp;]+)$" />
    </conditions>
    <action type="Redirect" url="pobocky/{C:1}" appendQueryString="false" />
    

  • Amir Khan 1287 posts 2744 karma points
    Jan 07, 2019 @ 19:32
    Amir Khan
    0

    Pasting isn't working for some reason so here's a screengrab.

    enter image description here

Please Sign in or register to post replies

Write your reply to:

Draft