This renders all the urls with trailing slash. But what can we do to enforce If user enter a url as www.site.com. It should redirect to www.site.com/.
I am using CatalogRouteHelper.MapDefaultHierarchialRouter for routing
I don't think we have that feature builtin, but if you want then it should be quite easy to implement in .NET via overriding TryGetVirtualPath(HttpContextBase context, CatalogContentBase content, string language, out string virtualPath) in HierarchicalCatalogPartialRouter, just add a trailing slash to the virtualPath before returning.
CatalogRouteHelper.MapDefaultHierarchialRouter simply register HierarchicalCatalogPartialRouter so it also can be customized to register new class inherited from HierarchicalCatalogPartialRouter.
To control out going urls you can simply assign property ContentRoute.UseTrailingSlash but I guess you want to redirect urls not having a trailing slash?
I also guess you only will redirect when the request is for a content item (e.g. you do not want to redirect http://site/resource/myscript.js to http://site/resource/myscript.js/)
What you can do is add an event handler to ContentRoute.CreatedVirtualPath
In the eventHandler you can check args.RoutingSegmentContext.RequestUrl and if it does not have a traliling slash you can do a redirect by calling args.RoutingSegmentContext.PermanentRedirect()
Consider though if you want to redirect media requests such as image.png, if not you might want to check if the request has an extension before doning the redirect.
Many Thanks guys, some time things can be more simpler then we think
CMS v 7.19.2
I don-t get the IIS rewrite 2.0 to work other then the root page. It makes eternal redirects on all other pages.
I want to remove trailing slashes on mediafiles. shouldn't that automaticly? Any suggestions?
I've had an opposite task - removing the trailing slashes and 301 redirecting any URLs with trailing slashes .
I think syntax has changed in the latest version (9.7.1 at the moment). This is the code I used to achieve the same without using IIS rewrite 2.0.
public class RoutingConfig : IInitializableModule
public void Initialize(InitializationEngine context)
ContentRoute.UseTrailingSlash = false;
ContentRoute.CreatedVirtualPath += ContentRoute_CreatedVirtualPath;
private void ContentRoute_CreatedVirtualPath(object sender, UrlBuilderEventArgs e)
var context = e.RequestContext.HttpContext;
if (context != null)
string url = context.Request.Path;
// if URL has a trailing slash and is not the homepage
if (!string.IsNullOrEmpty(url) && url.EndsWith("/") && url.Length > 1)
// only perfomr the redirect for front-end, don't affect EPiServer edit mode
if (context.Handler is System.Web.Mvc.MvcHandler)
public void Uninitialize(InitializationEngine context)
ContentRoute.CreatedVirtualPath -= ContentRoute_CreatedVirtualPath;
public void Preload(string parameters)
My only conern was detecting EPiServer edit mode, and not applying the same 301 redirect in there. This is why I have the check context.Handler is System.Web.Mvc.MvcHandler, but hopefully there is a more reliable way to only apply this rule to front-end.
This post shows you a reliable way of working out how/when your content is being rendered so allows you to work out if you are in edit mode or not: http://world.episerver.com/blogs/Linus-Ekstrom/Dates/2014/2/The-three-rendering-modes-of-EPiServer/
Thank you David,
In my context however this check wasn't enough. For example, for URLs like /EPiServer/cms/Stores/contentstructure/176 RequestSegmentContext.CurrentContextMode == ContextMode.Default is true. That is why I was checking the context handler instead. For those URLs the handler is EPiServer.Shell.Services.Rest.RestHttpHandler and there is no need to make any SEO redirects.
Hope this makes sense.
All makes perfect sense, thanks for sharing :)!