How can we redirect link to trailing slash

Vote:
 

Hi,

http://world.episerver.com/Blogs/Johan-Bjornfot/Dates1/2013/12/Routing-changes-in-75/

http://world.episerver.com/Modules/Forum/Pages/Thread.aspx?id=112523

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

Regards

Khurram

#113080
Nov 12, 2014 15:17
Vote:
 

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.

Regards.

/Q

#113094
Nov 13, 2014 7:32
Vote:
 

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.

#113109
Nov 13, 2014 11:17
Vote:
 

Many Thanks guys, some time things can be more simpler then we think

http://www.microsoft.com/en-gb/download/confirmation.aspx?id=7435

http://thatsit.com.au/seo/tutorials/how-to-fix-canonical-issues-involving-the-trailing-slash

Regards
Khurram

#113205
Edited, Nov 14, 2014 16:55
Vote:
 

Hi all,

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?

Regards

#120131
Apr 10, 2015 14:21
Vote:
 

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.

[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
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)
				{
					context.Response.RedirectPermanent(url.TrimEnd('/'));
				}
			}
		}
	}

	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.

#145635
Mar 08, 2016 16:02
Vote:
 

Hi Dasha

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/

David

#145637
Mar 08, 2016 17:12
Vote:
 

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.

#145638
Mar 08, 2016 17:32
Vote:
 

Hi Dasha 

All makes perfect sense, thanks for sharing :)!

David

#145656
Mar 09, 2016 10:33
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.