Try our conversational search powered by Generative AI!

How to check if a page is redirected from another page?

Vote:
 

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.

#174378
Jan 25, 2017 10:54
Vote:
 

I would recommend removing code like that and let simple and external URL be handled by the product in the default manor. If you need to serve "lower level" content on a "higher level" URL path there are some routing concepts that are better to take a look at, some examples are here: http://joelabrahamsson.com/custom-routing-for-episerver-content/

#174414
Jan 25, 2017 22:42
Vote:
 

Hi Johan,

i think you misunderstood what i'm trying to do..

Are you familiar with the "simple address" property in episerver. With that you can make a short url for the page. Default epi behaviour is that the page exists on 2 urls. THe short and the normal (full url). 

But the client want to have a redirect(301) from the short url to the full url always. Epi doesnt support that, so thats why i did the function. (like this ).

#174426
Jan 26, 2017 8:51
Vote:
 

In this particular case, a simple solution could be to install the 404 handler and add redirects there, and not use the simple address at all

#174431
Jan 26, 2017 10:02
Vote:
 

Maybe I misread a little but still stand on not touching the default behaviour. I have usually solved such redirect  requirements using some redirect/404-management or just instruct the editor to put another page on the root level and either do shortcut-settings on it or let the editors have a "Redirect Page Type" where you just select the target page and status code in a couple of properties. It might clutter the page tree in some cases but you get a better overview and better relation handling if someone tries to delete something. No one ever finds the Simple Addresses report anyway.

#174436
Jan 26, 2017 11:13
Vote:
 

If they are worried about duplicate content for SEO purpose, add a canonical link instead of rewriting routing. 

Use the existing shortcut functionality to set up redirects, simple urls etc if possible.

Install either 404 handler or SEO Manager if you need support for custom 301s. 

I would also recommend staying away from messing with the routing too much. It can easily blow up in your face :)

What need are you actually trying to fix? SEO?

#174438
Jan 26, 2017 11:33
Vote:
 

@Daniel... yes initially it was for SEO purposes, but we ofcourse have canonical in place. It was said by a third party SEO company which did a review of the site, to have it redirected to the full url also, which would somehow be better.. 

But thanks for all your opinions.. I will discuss this with the client and i'm sure we will find a better way... 

#174448
Jan 26, 2017 14:01
Vote:
 

I would go with canonical url imho :)

#174458
Jan 26, 2017 15:03
* 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.