HI,
i have the following scenario:
Once our client wanted to give a simple address to a page, which then needed to be redirected (301) to the full url.
So i created a nice little function to do that redirect which i added to the OnActionExecuting of my baseControllerPage.cs controller. like so:
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
Response.BufferOutput = true;
// Get current page
var currentPage = this.PageRouteHelperRepository.Page as SitePageData;
if (currentPage != null)
{
//checks if the currentUrl is simple address then 301 redirect to page.
RedirectIfCurrentUrlIsSimpleAddress(filterContext.RequestContext, currentPage);
}
base.OnActionExecuting(filterContext);
}
private void RedirectIfCurrentUrlIsSimpleAddress(RequestContext requestContext, SitePageData currentPage)
{
if (PageEditing.PageIsInEditMode)
return;
string simpleAddress = currentPage.ExternalURL;
if (!string.IsNullOrWhiteSpace(simpleAddress))
{
var requestedPath = requestContext.HttpContext.Request.RawUrl;
if (!simpleAddress.StartsWith("/"))
{
simpleAddress = string.Concat("/", simpleAddress);
}
//if (!simpleAddress.EndsWith("/"))
//{
// simpleAddress = string.Format("{0}/", simpleAddress);
//}
if (string.IsNullOrEmpty(requestedPath))
return;
if (requestedPath.Equals(simpleAddress, StringComparison.OrdinalIgnoreCase))
{
// Should redirect
var fullUrl = currentPage.ContentLink.GetFriendlyUrl(true);
if (!string.IsNullOrEmpty(fullUrl))
{
if (fullUrl == simpleAddress)
return;
requestContext.HttpContext.Response.RedirectPermanent(fullUrl);
}
}
}
}
Ok so that was working fine.. Until the client did the following.
Page "Ferrari" sitting somewhere on the site, which has a simple address called "nice-car". So going to http://localhost/nice-car would redirect my to the full url given the above functon.
Now they created another page called "Car", which has a simple address called "car" AND they have added a "EPiserver shortcut to another page in CMS" property to that "Ferrari" page.
So what happens now is when someone goes to the simple address of the "Car" page like so http://localhost/car , they stay on this url, but indeed see the "Ferrari" page instead of redirecting. That is because when you request this "http://localhost/car" page, and you come into the OnActionExecuting function of the BasePageController.cs my "currentPage" property is already the redirected "Ferrari" page.
So i cannot just add a "else" statement to just redirect to the currentPage, otherwise i will get infinite redirect loop. How can i tackle this problem in a nice way?
thanks in advance.