Copied to clipboard

Flag this post as spam?

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


  • Kris Anderson 12 posts 32 karma points
    Jan 04, 2011 @ 05:17
    Kris Anderson
    0

    Can I structure my URL's like this?

    Hey everyone,

    I was wondering if it's possible to change the URL structure slightly on a new Umbraco install. Currently, the site displays URL's like this:

    /about.aspx
         /about/where-we-are.aspx
         /about/what-we-do.aspx

    But I would like to display them like this:

    /about/
         /about/where-we-are.aspx
         /about/what-we-do.aspx

    So basically... top level directories end in a "/" instead of ".aspx."

    I'm thinking I could create a Yes/No checkbox under the properties section that would say something like "Show trailing '/' instead of '.aspx'?" Then, for each parent node where I want it to look like a folder instead of a document, I could check the yes box and Umbraco will automatically display that URL with a trailing "/" instead of ".aspx".

    That much I understand and can do... but where would I go about actually changing the code to make this happen? Is there a place in Umbraco where I can see where it adds the .aspx extension... and I can then run some sort of if/else statement to acomplish what I'm looking to do?

    Maybe this has something to do with NiceUrl? I see that being called in the default navigation structure (XSLT) that Umbraco uses.

  • Sascha Wolter 615 posts 1101 karma points
    Jan 04, 2011 @ 06:32
    Sascha Wolter
    0

    Hi Kris,

    *** as Rich rightfully pointed out the urls do all work, my mistake! ***

    although not impossible (aka a bit tricky) to achieve I would definitely vote against mixing and matching. What drives the whole thing with the directories as Urls is the UrlRewriting module, and this will get really confused if you just trying to do the above without assisting it, and that means writing a few lines or more .Net code.

    In your case I'd probably set up Umbraco to go with directories as Urls (using /about and /about/where-we-are), when a user clicks on a link to /about/what-we-do.aspx the page won't be found, in which case you can write your own bit of code in the 404 page/handler to try and look up /about/what-we-do instead. If it is found then yay redirect to that page, otherwise you've got a proper 404 (page not found). Alternatively you can write your own module that works before the UrlRewriter, catching .aspx page requests and transforming them in not-.aspx requests. In either case you're doing extra rounds to or on the server, which is far from ideal.

    What exactly speaks against not having .aspx at all?

    Hope that helps,

    Sascha

  • Kris Anderson 12 posts 32 karma points
    Jan 04, 2011 @ 08:28
    Kris Anderson
    0

    Hi Sascha,

    Thank you for the reply. I'm not against .aspx exensions. I just follow as many best practices for SEO reasons, and one of the often overlooked things is site structure.

    When a search engine sees:

    /about.aspx
       /about/who-we-are.aspx

    They see that the about.aspx page is un-related to the /about/who-we-are.aspx page (since it resides in a different directory). But when it is structured like this:

    /about/
         /about/who-we-are.aspx

    Search engines will say the /about/ folder is the main about page, and everything else in that folder is related to the /about/ sub-directory. At the end of the day it's not a dealbreaker to have a less than perfect structured website. But at the same time, when you're talking about doing everything you can for SEO reasons, you might as well try and fix the structure if it is possible.

    I'm against having server's do extra work. I don't want pages to be requested with one URL, and then have the server 404 and redirect to another URL. I was just hoping there may be an easy way to impliment what I am looking for. Umbraco creates these pages on the fly, and simply adds a .aspx extension onto the end of them. From what you said, you can also choose to not have the .aspx extension added. Where about within Umbraco can I see where that is taking place? Because if I can find that, couldn't I setup an if statement to check for a yes/no property I defined, and if I set that property to display the folder structure instead of .aspx, it just serves the /about/ folder instead? That should all be done once without any double processing on the server to serve the correct URL.

    So basically, the entire site would run like it currently does. Except I'd have say 5 root folders where I would add a new yes/no property where I can manually choose to display the page as a folder (/about/) instead of a document (/about.aspx). So Umbraco would still serve URL's like it does, except there would be a check in place to see if the property I setup is selected or not. If it is selected, it would choose to add "/" instead of ".aspx" to the end of that node/page.

  • Rich Green 2246 posts 4008 karma points
    Jan 04, 2011 @ 10:40
    Rich Green
    1

    Just to note, I'm pretty sure that if you have directory URL's on then all of the following will work:

    /about

    /about/

    /about.aspx

    /about/who-we-are

    /about/who-we-are/

    /about/who-we-are.aspx

    Rich


  • Kris Anderson 12 posts 32 karma points
    Jan 04, 2011 @ 21:14
    Kris Anderson
    0

    Hi Rich,

    They do all work. I'm just trying to figure out a way to make the default navigation display them the way I want.

  • Rich Green 2246 posts 4008 karma points
    Jan 04, 2011 @ 22:23
    Rich Green
    1

    Hi, You can turn directory urls on and then use NiceUrl and append a trailing slash. Should be easy to write an xslt extension to do this. Rich

  • Sascha Wolter 615 posts 1101 karma points
    Jan 04, 2011 @ 23:17
    Sascha Wolter
    0

    Hi Kris,

    sorry for the confusion, Rich is of course right. The Xslt should work fine as well. How about something like 'if the node doesn't have children then append .aspx, otherwise /'?

    Cheers,

    Sascha

  • Kris Anderson 12 posts 32 karma points
    Jan 04, 2011 @ 23:37
    Kris Anderson
    0

    Hi Sascha and Rich,

    There must be a bug in this forum software. Because I typed out a long reply and posted just after Sascha. But I guess since my reply was started before Sascha's, but posted after he posted, my reply just disapeared.

    Anywho... I enabled directory URL's in my web.config file and that got rid of the .aspx extension. I then went into my Navigation.xlst and modified the code to add a trailing slash to the end of all the URL's. So right now, my directory structure looks like this:

    /about-us/
         /about-us/who-we-are/
         /about-us/what-we-do/

    So I'm moving in the right direction. All I need to do is get those child node/pages ending in .aspx.

    Sascha brings up a good point... and I think one way to do this would be to code the XLST to check for child nodes. If no child node exists, it will use the "/" instead of ".aspx" extension. That would work, but I'm looking for a little more control.

    In my Navigation.xlst file, I'm using the following line of code to generate URL's:

    <a href="{umbraco.library:NiceUrl(@id)}/">

    That line uses the directory URL and adds a trailing "/" to the end of it to make it look like it's in a directory. In theory, I should be able to surroud that with some sort of if statement. Such as:

    IF (a parameted I have set for each node) = Y THEN

    <a href="{umbraco.library:NiceUrl(@id)}.aspx">

    Else

    <a href="{umbraco.library:NiceUrl(@id)}/">

    End If

    Clearly my code isn't in XLST form... but I hope that makes it more clear about what I'm trying to acomplish. I'm new to XLST so hopefully you guys can help me out or tell me if this is even possible.

    For my normal Textpages, I created a new property under the Generic Properties tab. I called this property DisplayAsDirectory and gave it a True/False type. Now, on each textpage I create, I can choose whether I want it to have the .aspx extension, or the trailing slash with a simple mark of the checkbox on the generic properties tab.

    My question is, can I call that within XLST? I see that XLST can run IF statements, but can it run IF statements against a property I configured?

  • Sascha Wolter 615 posts 1101 karma points
    Jan 05, 2011 @ 00:24
    Sascha Wolter
    0

    Hi Kris,

    here's some Xslt to get you started:

    <a>

      <xsl:attribute name="href">

        <xsl:choose>

          <xsl:when test="string(DisplayAsDirectory) = '1' or count(@isDoc) &gt; 0">    <!-- use directory if DisplayAsDirectory is set or it has children -->

            <xsl:value-of select="concat(umbraco.library:NiceUrl(@id), '/')" />               <!-- though I don't think the '/' is actually necessary here -->

          </xsl:when>

          <xsl:when test="count(@isDoc) = 0">                   <!-- can also use :otherwise here if it's just that... -->

            <xsl:value-of select="concat(umbraco.library:NiceUrl(@id), '.aspx')" />      

          </xsl:when>

        </xsl:choose>

      </xsl:attribute>

      <xsl:value-of select="@pageName" />

    </a>

    You will most likely need to modify this a bit to make it work with your solution. Also not sure if the automatic detection and overwrite parameter in conjunction work as I've outlined above, you might need to modify the logic there as well (e.g. in my code if you leave DisplayAsDirectory unticked and the node has children it will still be displayed as .aspx).

    As Rich mentioned it would make sense to put that in a global Xslt file as you might use it regularly, you can also think about writing your own Xslt Extension so you can easily call my.library:MyNiceUrl(int) and it will append .aspx or not based on your logic.

    Hope that helps,

    Sascha

  • Kris Anderson 12 posts 32 karma points
    Jan 05, 2011 @ 00:37
    Kris Anderson
    0

    Sascha... thank you SOOOO much! I modified the code a bit but got it working perfectly. Thank you for taking the time to help me figure this one out.

    I'm more impressed with Umbraco every day... it's really a great framework for a very customizable CMS.

  • Kris Anderson 12 posts 32 karma points
    Jan 19, 2011 @ 04:29
    Kris Anderson
    0

    I just created a new site using the latest 4.6.1 and I'm running into an issue.

    Previously I had this working with 4.5.2. When I enabled directory URL's in the web.config file Umbraco removed the ".aspx" extension and left all URL's as directories without a trailing "/". URL's would look like:

    /about-us
    /contact-us

    However, now with 4.6.1, it looks like it's adding a trailing slash to the end of the URL's. My URL's are now looking like:

    /about-us/
    /contact-us/

    That's good (because that's how a directory should look), but it's messing up how my URL's were working. In 4.6.1 when I want something displayed as a directory, it shows up as:

    /about-us//
    /contact-us//

    and if I want it dislayed with the .aspx extension it shows up as:

    /about-us/.aspx
    /contact-us/.aspx

    The easy fix, is how do I tell Umbraco to not add that trailing "/" to the end of the URLs when I have directory URLs enabled in the web.config file?

Please Sign in or register to post replies

Write your reply to:

Draft