Kim Andersen
Kim Andersen

Reputation: 1915

Rewrite URL parameters in Sitecore

I have a site, that shows an archive of news based on a "year"-parameter in the URL. An example could be this: domain.com/news/newsarchive?year=2008

How can I, in Sitecore CMS, rewrite my URLs to remove the "year"-parameter? I want to rewrite my URLs to something like this: domain.com/news/newsarchive/2008

How is this possible in Sitecore?

Upvotes: 2

Views: 3515

Answers (6)

Martijn van der Put
Martijn van der Put

Reputation: 4082

The best approach for this is as Jens Mikkelsen said 1) make the structure of your contenttree resemble the preferred url structure. and if this is not possible 2) use a wildcard item (an item with a * as name) as the childitem of the newsarchive-item. This item handles all children below the newsarchive-item that can't be found in the tree. In the codebehind of the sublayout you can read the path of the requested item and handle your code according to that.

Upvotes: 0

Sandbeck
Sandbeck

Reputation: 366

Just to add to Jens Mikkelsen 2nd point in his reply.

Make your own HttpRequestProcessor like this:

public class CustomResolver : HttpRequestProcessor
{
    public override void Process(HttpRequestArgs args)
    {
        Assert.ArgumentNotNull(args, "args");
        //make sure you are in the right context, and that the pipeline hasn't resolved an item yet
        if ((Sitecore.Context.Item == null && Sitecore.Context.Database != null) 
            && (Sitecore.Context.Database.Name != "core") )
        {
            var requestUrl = args.Context.Request.RawUrl; // e.g. domain.com/news/newsarchive/2008
            //Do your magic here
            Item item = ResolveItemOnUrl(requestUrl);
            if (item != null)
            {
                Sitecore.Context.Item = item;
            }
        }
    }
}

And add this to the HttpRequestPipeline after the ItemResolver

<pipelines>
  <httpRequestBegin>
    <processor patch:after="*[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']" type="yourNameSpace.CustomResolver,  yourNameSpace" />
  </httpRequestBegin>
</pipelines>

This is about the same way, Sitecore uses in Sitecore Ecommerce Services(SES) to resolve products.

Upvotes: 2

Jan Hebnes
Jan Hebnes

Reputation: 160

Adding to potential solutions: isapi_rewrite module on IIS level.

E.g. The module from helicontech can handle quite complex rewrites http://www.helicontech.com/isapi_rewrite/doc/examples.htm

This would be the right solution if the reason for the redirect was to handle urls targeting an earlier website structure. This would keep the sitecore implementation free of "legacy" references.

Upvotes: 0

Steven Newstead
Steven Newstead

Reputation: 1243

You can do exactly this now with the following open source module written by Sitecore MVC Stephen Pope:

https://github.com/Sitecore/Sitecore-Mvc-Routing

Upvotes: 0

Jens Mikkelsen
Jens Mikkelsen

Reputation: 2712

It kind of depend on your solution. The URL is by default an indicator of what item is resolved. This means that the URL /news/newsarchive/2008 normally would resolve an item in your content tree with that path.

Therefore you have several options.

  1. Make your content tree resemble the URL you want. So move your news around, so that all news created in 2008 is under a 2008 folder. The 2008 folder would then have a presentation showing the archive for that year in particular.

  2. Override the way your context item is resolved. This is done in the httpBeginRequest pipeline in the processor ItemResolver as fare as I remember.

  3. Use a wildcard as Yan suggest, and the have some custom code to get the year from your URL.

Hope that helps

Upvotes: 3

CGiddings
CGiddings

Reputation: 29

You can use the Wildcard approach.

Upvotes: 0

Related Questions