Installation and deployment
Version 3.x > Installation and deployment > 404 & Friendly URLs View modes: 
User avatar
Member
Member
danielrichard-oaot - 11/12/2008 8:19:27 AM
   
404 & Friendly URLs
I've set the 404 and 405 custom error pages to /cmspages/handler404.aspx in order to render extensionless URLs. That part is working great.

What I'd like to do now is create a custom "page not found" with our corporate branding, etc. instead of the default "The system cannot find the file specified." message that comes up with any invalid page URLs.

I assume since I've already pointed IIS to Kentico to handle 404 and 405 errors, that this is something I must do within Kentico?

I added some text to the /CMSMessages/Error.aspx and /CMSMessages/PageNotFound.aspx but that didn't seem to change anything.

User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 11/12/2008 8:57:52 AM
   
RE:404 & Friendly URLs
Hello,

Could you please try to create a custom error page and specify the path to it at CMS Site Manager -> Settings -> Web Sites -> Page not found URL. I think this may help you.

FYI: If you want to editany pager, you can find out what files are displaying the given content with the Firefox browser. "Right click on the given frame -> This Frame -> View frame info" - there is the path of the file, which contains the displayed page.

I hope this helps.

Best Regards,
Boris Pocatko

User avatar
Member
Member
danielrichard-oaot - 11/13/2008 11:33:08 AM
   
RE:404 & Friendly URLs
Thanks. That put me on the right path.

This isn't particularly Kentico related, but do you know of a way I can access the URL they attempted so I can do some logic on it (ie. if they tried to access a deleted news item, I can redirect them to our default news page).

HttpContext.Current.Request.Path just gives me the current "404Handler.aspx" page I created.

User avatar
Member
Member
danielrichard-oaot - 11/13/2008 11:38:01 AM
   
RE:404 & Friendly URLs
Oops... I guess this isn't working. If I type a URL with no extension my 404 page comes up fine. As soon as I add an extension (ie. thispage.asp) then the says "The system cannot find the file specified."

I'm using extensionless URLs in Kentico via IIS 404 and 405 redirects to /cmspages/handler404.aspx

User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 11/13/2008 12:06:54 PM
   
RE:404 & Friendly URLs
Hello,

Could you please go through the configuration settings of the extension-less urls once more? I tried it and I wasn't able to reproduce the error you are describing.

Configuration of extension-les URLs

Best Regards,
Boris Pocatko

User avatar
Member
Member
danielrichard-oaot - 11/13/2008 12:16:43 PM
   
RE:404 & Friendly URLs
I followed the steps again and noticed the Friendly URL Extension box was re-populated with ".aspx". So I cleared that out and hit Save again.

The instructions on the page you send say "Clear the Friendly URL extension textbox value to and click Save". I assume that means leave that box empty?

All my pages display with no extensions just fine. But the 404 page doesn't work if I put in an extension in the URL.

So, "www.mysite.com/thisisbad" goes to my custom 404 handler page. "www.mysite.com/thisisbad.aspx" just displays the "The system cannot find the file specified." message.

I put the following in the text box for Page not found URL in Site Settings: /404Handler (I created this page at the root of my site).

User avatar
Member
Member
danielrichard-oaot - 11/13/2008 12:27:11 PM
   
RE:404 & Friendly URLs
I made some headway. It seems the custom 404 page comes up fine for .aspx pages, but not for .asp, .htm, .html, etc. pages. I'd like it to work for anything erroneous that someone would type in.

User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 11/14/2008 6:40:31 AM
   
RE:404 & Friendly URLs
Hello,

Did you try to specify your error page "www.mysite.com/thisisbad.aspx" in the IIS settings (according to the Developer's guide) and in the CMS Site Manager -> Settings -> Web Sites -> Page not found URL textbox?

Best Regards,
Boris Pocatko

User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 11/14/2008 6:47:25 AM
   
RE:404 & Friendly URLs
Hello,

If this doesn't help, could you please write us an email to support@kentico.com?

Please include the following information.
1) The access to the site.
2) The urls you are trying.
3) the link to the error pages.

Best Regards,
Boris Pocatko

User avatar
Member
Member
lengland-thefellowship - 12/10/2008 3:43:22 PM
   
RE:404 & Friendly URLs
I have the exact situation as you and am interested in the solution.

1) Have configured IID and Kentico to use extension-less URLs

2) have configured a custom 404 page in Site manager

Invalid URLs ending with ASPX get the custom 404 page; invalid URLs with any other extension (.htm, .php, etc) get the generic "The system cannot find the file specified." page.

User avatar
Member
Member
danielrichard-oaot - 12/10/2008 8:21:07 PM
   
RE:404 & Friendly URLs
There is no built-in functionality to accomplish this as of Kentico 3.1a. I'm not sure if version 4 does this better or not.

What I ended up doing was redirecting 404 to my custom site page as mentioned in my previous post. That's "step 1" to take care of extensionless page errors and .aspx page errors. This does nothing for any other extensions. In addition, I wanted to redirect my old site page to their new ones (ie. /contacts.asp to /Contacts).

To accomodate that part, I edited the ~/app_code/global.asax.cs file in the Application_Begin request section. This is HARD-CODING every URL I want to redirect as well as manually capturing if the attempted URL is a .asp or .htm and redirecting them to the appropriate page.

The whole process was far from straight-forward and still isn't exactly a great solution, but it gets the job done at least. Reply back to remind me and I can post the code I added to the ASAX.CS file once I'm in the office as well as any more details you might need.

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 12/11/2008 5:06:01 AM
   
RE:404 & Friendly URLs
Hi,

My colleague was probably not aware of this option. At first, I just want to make sure that this is what you need.

You can configured to use extension-less URLs and now, when typing some page which does not exist, but with aspx extension (e.g. www.domain.com/blabla.aspx), you are redirected to the custom 404 error page.
But, when you type it without aspx extension (www.domain.com/blabla), you get the standard 404 error page.

If yes, you need to also modify the web.config file like following:
<customErrors defaultRedirect="~/CMSMessages/error.aspx" mode="On">
<error statusCode="404" redirect="~/customerrorpage" />

The mode is turned on and please note the value in redirect parameter - it is without extension.
I hope it will help.

Best Regards,
Juraj Ondrus

User avatar
Member
Member
danielrichard-oaot - 12/11/2008 6:48:23 AM
   
RE:404 & Friendly URLs
I don't believe handling 404 for pages with no extensions is the issue here. I believe the issue here is extensions that aren't ".aspx" or extension-less. For example, .asp, .htm, .gif 404s are not handled. I'm not sure if this is a Kentico issue or .Net issue.

Here's what I ended up with to handle any bad URLs for my site, as well as permanently redirect my older .asp pages to the new extensionless pages.

- In the IIS custom errors tab for your site, point 404 and 405 errors to /cmspages/handler404.aspx
- Create a custom error page (ie. ~/CMSTemplates/MySite/MyCustomError.aspx)
- Create a new Page Template in Kentico using MyCustomerError.aspx created previously
- Create a new Page in CMS Desk using the Page Template created previously (ie. /404Handler)
- In Kentico Site Manager / Settings / URLs, clear the "Friendly URL Extension" field to blank
- In Kentico Site Manager / Settings / Web Site, set the "Page not found URL" to the 404 handler page you created (ie. /404Handler)

All this accomodates the .aspx and extensionless 404 errors to your custom handler. This does nothing to accomodate .asp, .htm, .php, etc.

To do that, as well as redirect my old pages, I updated the global.asax.cs file's "Application_BeginRequest" event handler.

Here's what I added at the very beginning of that:
// This ignores the redirect for editing pages (CMS Desk)
if (CMSContext.ViewMode != ViewModeEnum.Edit){
string strUrl = HttpContext.Current.Request.Url.ToString().ToLower();
string strNewUrl = String.Empty;
bool badUrl = false;

// Get last four characters of the URL (used to manage non .aspx 404s)
string lastFour = String.Empty;
int startIndex = strUrl.Length - 4;
if (startIndex > 0) {
lastFour = strUrl.Substring(startIndex, 4);
}

// Redirect About menu
if (strUrl.Contains("/about/board_of_directors.asp")){
badUrl = true;
strNewUrl = "/AboutOAOT/BoardofDirectors";
}
// etc.... for all of your redirect pages

// This catches a few page extensions that would otherwise result in the "specified file cannot be found" message
// This just redirects to the home page in this case.
else if (lastFour == ".asp" || lastFour == ".htm" || lastFour == ".php"){
badUrl = true;
strNewUrl = "/";
}

if (badUrl){
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", strNewUrl);
}
}

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 12/11/2008 8:38:06 AM
   
RE:404 & Friendly URLs
Hi,

Could you please try to create the custom error file directly in the project directory (e.g. in some folder which will be excluded from the URL rewritting in Site Manager -> Settings) and set the IIS and Kentico to use this physical file? I think this could help.

Moreover, thank you for your solution, it will be helpful for other users.

Best Regards,
Juraj Ondrus

User avatar
Member
Member
danielrichard-oaot - 12/11/2008 11:50:37 AM
   
RE:404 & Friendly URLs
I tried putting my custom error pages in a folder called "Custom" off the site root. I pointed the IIS 404 to it as well as setting the "Page not found URL" setting in Kentico to "~/Custom/404Handler.aspx".

The .asp page still showed the "Cannot find the file specified" and this seems to have broke the extension-less 404 handling. But I probably didn't do it quite right either.

I have to modify my global.asax.cs for redirecting of old URLs anyway, so I'm fine with a few extra lines of code to manage non-aspx extensions.

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 12/12/2008 8:04:00 AM
   
RE:404 & Friendly URLs
Hi,

Thank you for your posts which are very helpful.

Just to be sure - have you added /Custom/404Handler.aspx to the Excluded URLs setting in Site Manager -> Settings -> URLs?

Best Regards,
Juraj Ondrus

User avatar
Member
Member
danielrichard-oaot - 12/12/2008 8:26:12 AM
   
RE:404 & Friendly URLs
No, I hadn't modified that setting. Unfortunately I just lost my Dev box today (gotta love lease returns!). It'll be a little while before I get another test environment setup for Kentico.

User avatar
Member
Member
danielrichard-oaot - 12/12/2008 1:00:20 PM
   
RE:404 & Friendly URLs
I tried reworking the solution as you advised, but it didn't work. How I have things setup right now works, so I'll stick with that unless you can provide a step-by-step on how to set this up within Kentico to handle extensions that are not .aspx.

Thanks.

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 12/17/2008 5:02:10 AM
   
RE:404 & Friendly URLs
Hi,

Here is the description. I managed it to work with .htm, html, php, gif, jpg, asp extensions (I have tested these extensions).

I have configured extensions-less urls as per instructions here - http://www.kentico.com/docs/devguide/configuration_of_extension_les.htm.

Then I have created a custom 404 page in CMS Desk under the root, named e.g. notfound (so the alias is ~/notfound). I have set this alias path in the Site Manager -> Settings -> Web Site -> Page not found URL with this value: ~/notfound

Then, I have modified the web.config file:
<customErrors defaultRedirect="~/CMSMessages/error.aspx" mode="On">
<error statusCode="404" redirect="~/notfound"/>
<error statusCode="403" redirect="~/notfound"/>
</customErrors>

Please note that the mode is turned on and also there is error for 403. This is because when I tried to display some page with asp extension, I got 403 - forbidden message.

I have tried above mentioned extensions, also non-existing page without extension and with aspx extension, and the custom page was always displayed.

Best Regards,
Juraj Ondrus

User avatar
Member
Member
danielrichard-oaot - 12/17/2008 7:29:42 AM
   
RE:404 & Friendly URLs
I tried this with no success. I never get a 403 error, but it did replace the "Specified file cannot be found" with a 404 page... just not my custom 404 page as I hoped. So I believe our IIS may be setup differently. I did some research and I believe part of the problem is that non-aspx pages are not getting processed by the .Net framework, therefore they never fall into the error trapping that I setup.

I tried setting the .asp extension (in my IIS site Home Directory / Configuration) to use the .Net framework (copied the .dll path from .aspx extension), but that just replaced the "Specified file cannot be found" with an even more generic IE error page that contained no error code.

Also, in my web.config "~/404Handler" wouldn't work. I had to leave out the "~". It worked fine for .aspx pages and extensionless pages. But still not for any other extensions.

User avatar
Member
Member
smaxwell-specialtysystems - 1/16/2009 3:44:33 PM
   
RE:404 & Friendly URLs
If the only issue is the .aspx not working while .asp and .htm are then check IIS to make sure the ASP.NET web service extension is allowed. The default setting is prohibited for this in IIS 6.

User avatar
Member
Member
Mufasa - 1/21/2009 4:06:02 PM
   
RE:404 & Friendly URLs
The problem with this approach is that Kentico (3.1a anyway) redirects the user to the specified page. This has two problems:
- The URL is now modified. The error page can not use the URL to try and figure out possible alternatives for the user. (The referrer field could be used in theory, but that can not be relied upon to be set by all user agents.)
- Since the error page is really just shown as a regular page, it no longer sends an HTTP 404 response, but instead of 200 (or similar) OK response. It now looks to crawlers (and other similar user agents) that the original bad URL was simply a redirect to the error page and that it should be treated as valid, which is definitely the wrong behavior.

Also, this method limits to 404 errors only, correct? What about 403, and the other common error codes?

So, because of these reasons we can't use the built-in Kentico custom error pattern with extension-less URLs.

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 1/22/2009 6:07:16 AM
   
RE:404 & Friendly URLs
Hi,

You can handle the 404 request in global.asax.cs file in Application_BeginRequest().

You can determine if the requested URL returned 404 error and then stop the request.
I hope it makes sense.

Best Regards,
Juraj Ondrus

User avatar
Member
Member
Mufasa - 2/3/2009 11:02:52 PM
   
RE:404 & Friendly URLs
I tried that, but it got tricky. What I found to work best for me (with Kentico 3.1a):

Site Manager->Settings->Web Site->Page not found URL: ~/Errors/404.aspx
Site Manager->Settings->URLs->Excluded URLs: ...;/Errors

Created the above directory and files directly in the file system (in other words, not with the Portal engine/the Content tab). Then added the following to the new 404.aspx page:
    protected void Page_Load(object sender, EventArgs e)
{
Response.Status = "404 Not Found";
}

User avatar
Member
Member
Mufasa - 2/3/2009 11:31:58 PM
   
RE:404 & Friendly URLs
Oh yeah, and since I'm using extension-less URLs and IIS 6, I had to add the following to the ~/App_Code/Global/CMS/CMSRequest.cs:BeforeBeginRequest() method:
 // Check for ASP pages and assume a 404 error
if (CMS.URLRewritingEngine.URLRewriter.CurrentURL.EndsWith(".asp"))
{
string notFoundUrl = CMS.GlobalHelper.ValidationHelper.GetString
(CMS.SettingsProvider.SettingsKeyProvider.GetStringValue(CMS.CMSHelper.CMSContext.CurrentSiteName + ".CMSPageNotFoundUrl"), "");

// if exist redirect this page to not found url
if (notFoundUrl != "")
{
HttpContext.Current.RewritePath(notFoundUrl, HttpContext.Current.Request.PathInfo, "aspxerrorpath=" + CMS.URLRewritingEngine.URLRewriter.CurrentURL);
}
}

Add whatever other conditions you need in the opening if statement. Note, that the way I wrote this, anything that satisfies the condition will be a 404 error. Not the most robust, but messing with the Application_BeginRequest method in ~/App_Code/Global.asax.cs was getting too complicated and too easily could create infinite loops or bad URLs depending on the setup.

1 2