+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Redirect Non-existent HTML,HTM ASP pages?

Redirect Non-existent HTML,HTM ASP pages?

10 posts, 0 answered
  1. Randy
    Randy avatar
    19 posts
    Registered:
    13 Nov 2007
    12 Feb 2009
    Link to this post

    We spent two days on this so I felt I had to tell the world how it was accomplished.


    Issue:

    Someone links to contact.htm on your site. You do not have a contact.htm, but you do have a contact.aspx.



    Solution

    Step 1: Configure IIS

    What happens if you request a non-existent HTM,ASP page?

    By default you will get a canned "white page". When you install the .NET Framework is maps certain file extensions to the ASP.NET ISAPI, aspnet_isapi.dll. Neither HTML nor HTM files are mapped to it (because they are not ASP.NET pages). However, you can configure IIS to treat them as ASP.NET pages and serve our custom error pages.

    Run the IIS Manager

    Select a web application

    Right click and go to Properties

    On the Virtual Directory tab click Configuration

    In the Extension column find .aspx, double click and copy the full path to aspnet_isapi.dll. It should be something like C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\«

    aspnet_isapi.dll

    Click Add and paste the path in the Executable box

    In the Extension box type .html

    Make sure Check that file exists is NOT checked

    Dismiss all dialogs


    Step 2: Modify Web.Config

    Add as many types as you want into the httphnadler section of your web.config.

    <httpHandlers>

    <add path="*.htm" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>

    <add path="*.html" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>

    <add path="*.asp" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>

    </httpHandlers>


    Step 3:  Set Default Page Properties

    • Open Sitefinity Admin
    • Open the Contact.aspx file.
    • Click Properties
    • Expand More Options
    • Add another URL (~/Contact.htm)
    • Check the “Redriect to Default URL” box
    • Save Changes.

    Step 4: Write code using Telerik API

    Add this bit of code to your global.asax

    void Application_Error(object sender, EventArgs e)

    {

    Exception lastException = this.Server.GetLastError();

    if (lastException is HttpException)

    {

    HttpException httpException = (HttpException)lastException;

    if (httpException.GetHttpCode() == 404)

    {

    // get the name of the requested page - just the /thisPage.htm or /DIR/thisPage.asp etc

    string pageName = string.Concat("~", Request.Url.AbsolutePath.ToString());

    // create a new instance of CmsManager

    Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();

    // let's get the page by passing one of it's additional url's

    Telerik.Cms.IPage myPage = cmsManager.GetPageByAdditionalUrl(pageName);

    // we can cast myPage as ICmsPage and redirect to the Default URL

    if (myPage != null)

    {

    Response.Redirect(((Telerik.Cms.ICmsPage)myPage).DefaultUrl.Url);

    this.Server.ClearError();

    }

    }

    }

    }



    Now if you come to the site with contact.htm and it doesn’t exist, you will be redirected to contact.aspx.   

    I hope this helps
     

  2. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    13 Feb 2009
    Link to this post
    Hello Randy,

    Thank you for willingness to help others, and share your experience. Indeed, this is the way to handle requests to non existing files/pages. The extension should be really mapped to the ASP ISAPI,but for the map should be:
    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727.

    We have updated your Telerik account for this.

    Kind regards,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Adam @Habanero
    Adam @Habanero avatar
    45 posts
    Registered:
    22 Jun 2012
    30 Apr 2009
    Link to this post
    Hi guys

    This seems like it *might* work for our problem.

    We are on SF 3.6 SP1 and have the same issue, but we are using IIS7

    sf404.aspx is cofigured as the error page for our application in IIS.
    in the web.config we have our custom error page wired up to 404 errors "~/404.aspx".

    I tried adding a managed handler for html/htm/asp files and can't get it to work. I just get this error

    Could not load type 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\*'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.Web.HttpException: Could not load type 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\*'.

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace: 

    [HttpException (0x80004005): Could not load type 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\*'.]   System.Web.Compilation.BuildManager.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase) +11204048   System.Web.Configuration.HandlerFactoryCache.GetHandlerType(String type) +23   System.Web.Configuration.HandlerFactoryCache..ctor(String type) +25   System.Web.HttpApplication.GetFactory(String type) +91   System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +338   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +263


    Version Information: Microsoft .NET Framework Version:2.0.50727.3074; ASP.NET Version:2.0.50727.3074

    Also, it is not clear how this will work for pages that are requested with either the wrong extension (exampl.asps) by accident, or just a random url like http://site/d  that seems like it would still go to the generic iis 404 page.



  4. Adam
    Adam avatar
    63 posts
    Registered:
    30 Jan 2008
    30 Apr 2009
    Link to this post
    I may be missing the point... What is the advantage to this method as opposed to setting up a simple 301 redirect for the contact.htm page in IIS? 
  5. Randy
    Randy avatar
    19 posts
    Registered:
    13 Nov 2007
    01 May 2009
    Link to this post
    Adam, we wanted the give the content editors the ability to add alternate pages for htm, txt, asp whatever....  We work in a corporate environment with limited or no access to the production servers.  This gave the the editors total control.
  6. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    04 May 2009
    Link to this post
    Hi,

    Adam, I am not sure why you have mapped an extension to path like
    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\*
    The right map should be to:
    C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll
    In other words, the request should be pointed to exact dll file that should process it.

    Kind regards,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  7. Adam @Habanero
    Adam @Habanero avatar
    45 posts
    Registered:
    22 Jun 2012
    04 May 2009
    Link to this post
    We got this working in production since our prod server is running iis6.  But even with the proper dll in the path I had issues getting it to work on IIS7.  Could just be something I did wrong though.
     
    Working great now.

    The only difference we made was to map all extensions to asp.net instead of just specific ones.
  8. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    05 May 2009
    Link to this post
    Hello Adam,

    I am glad you managed to get it working. I am only wondering what was the difference in IIS7 though. Was your project running in Integrated mode, or in Classic mode? I would like to perform some tests, so we could fix the problems if they are presented with IIS7.

    Sincerely yours,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  9. Adam @Habanero
    Adam @Habanero avatar
    45 posts
    Registered:
    22 Jun 2012
    05 May 2009
    Link to this post
    Hi Georgi,

    I got the error I posted above.  My site was running in Integrated mode.
  10. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    06 May 2009
    Link to this post
    Hi Adam,

    Alright, I will follow up if I find something weird with IIS7 in Integrated Mode.

    Regards,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Register for webinar
10 posts, 0 answered