Question

Currently I am trying to remove .aspx extensions from sitecore URL's. A blog suggests that changing the following would fix this issue:

<add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="true"

The problem is that this does not work for all code in our websites as some custom controls force the usage of .aspx.

What I was thinking is that if I can get into the pipeline before a response is sent back to the user I could change the URL to one that is more SEO friendly (no .aspx), I am trying to do this using the following code

public class CustomExecuteRequest : Sitecore.Pipelines.HttpRequest.ExecuteRequest
{
    protected override void PerformRedirect(string url)
    {
        base.PerformRedirect(url.Replace(".aspx" , ""));
    }

    public override void Process(Sitecore.Pipelines.HttpRequest.HttpRequestArgs args)
    {
        PerformRedirect(args.Url.FilePathWithQueryString.Replace(".aspx", ""));

        //args.Url.FilePathWithQueryString = args.Url.FilePathWithQueryString.Replace(".aspx", "");

        //base.Process(args);
    }
}

The code is being hit and this solution works, however it is performing a redirect which is really not what I want to do, I would rather just have a regular request with the URL cleaned up. The problem is that the args variable has a read-only field (commented out below) that is used to actually create the URL.

Does anyone have any ideas as to how I can change the URL here, or perhaps somewhere else in the pipeline?

Was it helpful?

Solution

What you are doing inside the HttpRequestPipeline is certainly NOT the correct way to change links. As with any web application, any links on the page are returned to the client as content of the HTTP request. Redirecting during the request will not change the rendered content.

The only way to do this is the hard way:

  • Find all the places in your source generating the links containing .aspx. A search for "aspx" inside the solution might help
  • Find all references to UrlOptions and to LinkManager and check if AddAspxExtension=true is set manually as urlOptions.

Something like this would always produce a .aspx ending for example:

var urlOptions = UrlOptions.DefaultOptions;
urlOptions.AddAspxExtension = true;

LinkManager.GetItemUrl(item, urlOptions);

OTHER TIPS

Either the hard way of changing all your links to use link manager or if you are strapped for time then suggest something maybe outside of sitecore and use IIS urlrewrite module.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top