James Walford
James Walford

Reputation: 2953

Which method to retrieve path portion of URL in Sitecore?

I'm writing an extension to the HTTPRequest pipeline to redirect incoming requests where Sitecore can find the right item but where the requested URL isn't formed exactly as Sitecore would form it. This is to prevent SEO duplicate content issues.

The portion of the URL I want to examine is the part that will conform to the LinkManager.GetItemUrl(context.item) result. In our case we have language embedded in the path, e.g.:

www.mysite.com/en-gb/stuff/things

So GetItemUrl returns /en-gb/stuff/things

I can't find the right method, either on the Sitecore.Pipelines.HttpRequest.HttpRequestArgs object, nor on the System.Web.HttpContext.Current.Request.Url object.

I can get the whole URL or the path minus language embedding. Which object.method will give me /en-gb/stuff/things?

Upvotes: 4

Views: 4928

Answers (2)

Paul George
Paul George

Reputation: 1817

This isnt a direct answer, but it may be useful nonetheless, as Sitecore throws a couple of tricks into the mix when doing what (I think) you're aiming to achieve.

From memory...

The language portion of the URL is removed by the strip language pipeline processor, if (and only if) the linkmanager is set with LanguageEmbedding 'always' or 'asNeeded'. If it is set to 'never' if does nothing to the url in the HttpRequest. This can mess with your path comparison if comparing a stripped path with a prefixed path from LinkManager.

You can add a pipeline step before the stripLanguage step which adds the incoming URL as you want it to the dictionary within HttpRequest.

public class StoreOriginalRequestUrl : PreprocessRequestProcessor
{
    public override void Process(PreprocessRequestArgs args)
    {
        args.Context.Items["OriginalRequestUrl"] = args.Context.Request.RawUrl;
    }
}

...you can then pick this up in a later step after the item resolver. I'm presuming this is what you're doing - comparing the request URL with the context item's URL from Linkmanager.

I think you can parse the linkmanager returned Url string as a Uri object and compare the path there. It seems to behave better when fed a fully qualified URL, so perhaps modify the UrlOptions returned from GetDefaultOptions() to provide one.

Paul

Upvotes: 2

Marek Musielak
Marek Musielak

Reputation: 27132

I'm not sure if I understood you correctly but it seems like what you're looking for is System.Web.HttpContext.Current.Request.RawUrl . If not, please explain what you want to achieve with more details.

Upvotes: 5

Related Questions