Sam
Sam

Reputation: 461

Sitecore Url works with any Extension

In my website , the url's are working even entering something after .aspx and giving staus code 200.

Eg: below is normal page with .aspx and status code 200.enter image description here

But even i have any random extension i got 200 status code which suppossed to be a 404 Status code, enter image description here

Any Help.

Upvotes: 2

Views: 1669

Answers (2)

anders laub
anders laub

Reputation: 104

Sitecore skips everything after the last dot ”.” in an url when attempting to resolve an item.

This is done by the class Sitecore.Web.RequestUrl which has a property called ItemPath.

This property attempts to create a valid path to an item from the requested url. It is not possible to override this property.

If you for some reason would like Sitecore to return a 404 status code if an item is requested with a file extension, such as .aspx, you could do something like this in a 404 not found item resolver.

See this post http://laubplusco.net/handling-404-sitecore-avoid-302-redirects/ the following method extends the one shown in the post.

protected virtual bool IsValidContextItemResolved(string filePath)
{
  if (Context.Item == null || !Context.Item.HasContextLanguage())
    return false;
  if (filePath.Contains(".") && !RequestIsForPhysicalFile(filePath))
    return false;
  return !(Context.Item.Visualization.Layout == null 
    && string.IsNullOrEmpty(WebUtil.GetQueryString("sc_layout")));
}

It is important to ensure that the requested url is not for a physical file first.

This is done by checking that the args.Url.filepath does not map to a physical file. The rule which I show here says that if an item has been resolved and the filepath contains a dot then the requested url should return a 404 and the context item should be the not found item. The code could be extended to check what comes after the dot to see if it is a valid extension.

Upvotes: 0

Martin Davies
Martin Davies

Reputation: 4456

Sitecore is quite generous when it resolves URL's. If you want to enforce correct extensions, you could create a custom Item Resolver which ensures the context item remains null in the process method if the URL has the incorrect extension.

Here's a helpful article on creating an Item Resolver:

Thoughts on httpRequestBegin - Custom Item Lookups

In my example below, the base process method is called. After that we check if the Context Item meets the requirements, and set it to null if not. (You'll need to implement TemplateIsAPageType and ExtensionIsValid as you see fit.)

public class CustomItemResolver : HttpRequestProcessor 
{
    public override void Process( HttpRequestArgs args ) 
    {
        base.Process(args);

        if( Context.Item != null && TemplateIsAPageType() && !ExtensionIsValid()) 
        {
            Context.Item = null;
        }

    }
}

Another approach might be something like this, where we compare the requested URL with the resolved item's 'ideal' URL:

public class CustomItemResolver : HttpRequestProcessor 
{
    public override void Process( HttpRequestArgs args ) 
    {
        base.Process(args);

        if( Context.Item == null) 
            return;

        var requestUrl = HttpContext.Current.Request.RawUrl;
        var idealUrl = LinkManager.GetItemUrl(Context.Item);

        if(requestUrl != idealUrl)
            Context.Item = null;
    }
}

Upvotes: 3

Related Questions