Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Diego 4 posts 74 karma points
    Mar 04, 2020 @ 09:35
    Diego
    0

    Redirect 301 Server Error

    Hello,

    I have an Umbraco environment version 7.9.0 and I have this code:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "Master.cshtml";
    
        // This page shows nothing, only redirects to index page.
       var indexPage = Model.Content.GetPropertyValue<IPublishedContent>("indexPage");
       if (indexPage != null)
       {    
             Response.Redirect(indexPage.Url, false);
       }
       else
       {
          var results = Model.Content.Children.Where(x => x.Name.ToLower() == "index");
           if (results.Any())
           {        
            Response.Redirect(indexPage.Url, false);
           }
       }
      }
    

    To put in context, I have two machines in two AWS servers and a AWS Load Balancer. The URL I am trying to reach is the URL of the load balancer and it decides which machine to use.

    The problem is the Response.Redirect is giving a 302 code and I need to get a 301 so I changed Response.Redirect to Response.RedirectPermanent. After changing it the server is thowing a 503 error but if I remove the cookies it works again, and if I remove the cookies it throws the error again and so on.

    Have you ever had this problem or something similar?

    Thank you

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Mar 04, 2020 @ 14:39
    Alex Skrypnyk
    1

    Hi Diego

    To make 301 redirect use this code:

    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location", currentUrl.Replace("http://mydomain", "http://www.mydomain"));
    Response.End();
    

    Btw, it's a bad idea to make redirects in views. Do it with IIS Rewrite Rules - https://our.umbraco.com/documentation/Reference/routing/iisrewriterules/

    Thanks,

    Alex

  • Diego 4 posts 74 karma points
    Mar 10, 2020 @ 08:21
    Diego
    0

    Hi Alex,

    I tried to do it with IIS Rewrite Rules and the problem persists. It redirects to the URL I want but when I delete the cookies it throws a 503 error and if I delete the cookies again it works and so on. Have anybody ever had a similar problem?

    Thank you.

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Mar 10, 2020 @ 21:42
    Alex Skrypnyk
    0

    Hi Diego

    What cookie?

    Alex

  • Diego 4 posts 74 karma points
    Mar 10, 2020 @ 21:47
    Diego
    0

    Hi Alex,

    The cookies are AWSALB and AWSALBCORS both from Amazon Load balancer.

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Mar 11, 2020 @ 18:20
    Alex Skrypnyk
    0

    I didn't know that Amazon Load balancer adds cookies to the website.

    Can you avoid it?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies