Preserving URLs of an existing site?

Topics: Developer Forum
Oct 8, 2008 at 12:03 PM
I am converting an existing file-based ASP.NET website to N2. One issue I'm having is preserving the existing URLs, which is very important for search engine optimization. For example, in my site I have URLs like http://mysite.com/foo/ which shows the default page in the /foo directory as well as URLs like http://mysite.com/foo/bar.aspx which shows the bar.aspx page in the /foo directory.

In N2, I need to add a "/foo" page and a "/foo/bar.aspx" page. To do this, I set <web extension="" /> in the Web.Config file and created a page called "foo". I could then succesfully navigate to http://mysite.com/foo and see content served from N2. Next, under the "foo" page I created a new page and put "bar.aspx" as the name. Unfotrunatly, N2 does not recognize this name and I get a 404 file not found error when navigating to http://mysite.com/foo/bar.aspx.

Does anyone know a solution to make this work?

Thank you -

-James
Oct 8, 2008 at 12:36 PM
For the purpose of preserving existing pages, you can use the following trick -- a "proxy" content item, which redirectes strait to the old page without a roundtrip to client:

[N2.Definition]
public class ProxyItem : AbstractContentPage, IStructuralPage 
{    
    [N2.Details.EditableTextBox("Url", 100)] 
    public new string Url { get { return (string)this.GetDetail("TemplateUrl"); } set { this.SetDetail<string>("TemplateUrl", value); } }
    public override string TemplateUrl { get { return this.Url; } }
}

 

This way, your old page will participate in N2 navigation, which is handy. On the other hand, such new item will be required for every existing old page.
Oct 8, 2008 at 3:31 PM
Edited Oct 8, 2008 at 3:32 PM
Is there no way to allow users to control the extension of pages? I don't think this solution will work in terms of SEO because it will appear there is duplicate content.
Oct 8, 2008 at 4:41 PM
I tried overriding the Extension property, but it does not work as I expected it to:
    public override string Extension
{
get {
int nameLastIndex = this.Name.Length-1;
int extensionIndex = this.Name.LastIndexOf('.');
if ((extensionIndex > 0) && (extensionIndex < nameLastIndex))
return this.Name.Substring(extensionIndex);
return base.Extension;
}
}
Any more ideas?
Coordinator
Oct 8, 2008 at 5:59 PM
You could try configuring observedExtensions:

<host><web observedExtensions=".aspx,.html"/>

If that doesn't work you could subscribe to the not found event and do some cleverness:

N2.Context.Current.UrlParser.NotFound += delegate... { if(e.url.endswith(".aspx") e.AffectedItem = TryToFindNewPage(); };
Oct 9, 2008 at 3:34 PM
Thank you again Libardo!

Here is the fix that worked, in full:

First I set the <web extension="" observedExtensions=".aspx,.htm,.html" /> in the Web.Config file.

Next, I overrode the Extension property of content item as described in my last post:
    public override string Extension
{
get {
int nameLastIndex = this.Name.Length-1;
int extensionIndex = this.Name.LastIndexOf('.');
if ((extensionIndex > 0) && (extensionIndex < nameLastIndex))
return this.Name.Substring(extensionIndex);
return base.Extension;
}
}
Now if someone adds a file extension of .aspx, .htm, or .html to the "Name" property this is automatically picked up. This causes a minor display issue on the edit screen but it is not a concern (it shows the extension both in the name edit and after).

I'm loving N2 more and more each day! Thanks!

-James
Mar 18, 2009 at 3:29 PM
This solution no longer works in the most recent release of N2. How can I get it to work?
Coordinator
Mar 18, 2009 at 5:12 PM
Good to know the context. Does this also affect external users?
Mar 18, 2009 at 5:44 PM
Good to know the context. Does this also affect external users?

No, it is only the Edit interface that is affected.
Mar 20, 2009 at 1:53 PM
Any solution yet?
Coordinator
Mar 20, 2009 at 6:20 PM
Sorry, I don't have a solution for you. Look in the other thread for a suggestion on how to change the code. The thing that's strange is that the url is excempt of extension even though it's included in the name. The url producer just uses item.Name + item.Extension to create the url.
Coordinator
Mar 21, 2009 at 3:53 PM
Hi again. I removed the extension filtering so if you work from the trunk version you should be able to remove the additional code in your content items and rely soly on the item's name. Also double-check the absence of any PreviewUrl property.
Coordinator
Mar 22, 2009 at 10:10 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jun 8, 2009 at 2:33 PM

Just to follow up, this issue was resolved. 

Everything works as we expected (I work with jamestharpe). We were also able to get rid of the extension override we wrote.