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开发者_StackOverflow社区 possible in Sitecore?
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.
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.
Override the way your context item is resolved. This is done in the httpBeginRequest pipeline in the processor ItemResolver as fare as I remember.
Use a wildcard as Yan suggest, and the have some custom code to get the year from your URL.
Hope that helps
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.
You can use the Wildcard approach.
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
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.
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.
精彩评论