Try our conversational search powered by Generative AI!

Url rewrite 302 redirecting trailing slash

Vote:
 

The Urlrewriter is producing a 302 redirect in a very specific scenario:

http://www.baigent.com/Portfolio is redirected to http://www.baigent.com/Portfolio/ in this way.

for SEO reasons we obviously would prefer this to be a permanent redirect... after initial research into this I'm not entirely sure if this is the the UrlRewrite's doing or if IIS is to blame.

Note the rewrite section is pretty much standard:

  <urlRewrite defaultProvider="EPiServerFriendlyUrlRewriteProvider">
    <providers>
      <add name="EPiServerFriendlyUrlRewriteProvider" type="EPiServer.Web.FriendlyUrlRewriteProvider,EPiServer" />
      <add description="EPiServer identity URL rewriter" name="EPiServerIdentityUrlRewriteProvider" type="EPiServer.Web.IdentityUrlRewriteProvider,EPiServer" />
      <add description="EPiServer bypass URL rewriter" name="EPiServerNullUrlRewriteProvider" type="EPiServer.Web.NullUrlRewriteProvider,EPiServer" />
    </providers>
  </urlRewrite>
 
Anyone encountered anything similar?
 
Regards,
JC 
#49858
Apr 04, 2011 9:07
Vote:
 

Found out about this today. Did you get anywhere with the investigation how to change this to 301?

#51728
Jun 21, 2011 13:39
Vote:
 

If you take a look at the method ConvertToInternalInternal inside the FriendlyUrlRewriteProvider

 

if (!url.Path.EndsWith("/"))
		{
			url.Path += "/";
			HttpContext.Current.Response.Redirect(url.Path + url.Query + url.Fragment);
		}

    Guess you can make your own and override that redirect

#51761
Jun 22, 2011 8:48
Vote:
 

Thanx Anders. It worked for Episerver 5 version. In 6R2 FriendlyUrlRewriteProvider is changed so I cannot find suitable location to check and add 301. If anybody has a clue, it would help.

Regards

#52400
Jul 21, 2011 13:58
Vote:
 
in cms6r2 i Think the method is trytopconverttointernal
#52404
Jul 21, 2011 18:25
Vote:
 

Hi, I have the same problem. Status code in case of missing trailing slash instead of 302 should be 301. Found place where trailing slash is analysed:

EPiServer.Web.FriendlyUrlRewriteProvider, ConvertToInternalInternal method

But I could not figure out the correct place to change status code. Can you please give me a clue.

 

Thanks in advance,

Artem

#53583
Sep 15, 2011 15:01
Vote:
 

Hi, same problem here. How did you manage to solve this problem? 

 

TIA.

#56803
Feb 09, 2012 11:28
Vote:
 

global.asax, EndRequest event

private void Global_EndRequest(object sender, EventArgs e)
        {
            //EPiServer in case of missing trailing slash in the url returns status code 302. This is not good for the SEO.
            //Url with missing trailing slash is exceptional on the page and should be fixed by renderer of such urls.
            //Found place where trailing slash is analysed: EPiServer.Web.FriendlyUrlRewriteProvider, ConvertToInternalInternal method
            /*
             if (page.LinkType == PageShortcutType.External)
                {
                    internalObject = new RedirectPageReference(page.PageLink);*****************************
                    url.Uri = new Uri(page.LinkURL);
                    return true;
                }
+++++++++++++++ if (!url.Path.EndsWith("/") && string.IsNullOrEmpty(UrlRewriteProvider.FriendlyUrlExtension)) +++++++++++++++
                {
                    internalObject = new RedirectPageReference(page.PageLink);*****************************
                    url.Path = url.Path + "/";
                    return true;
                }
             */
            //As you can see internalObject is initialized with RedirectPageReference, and objects of this class
            //is used in: EPiServer.Web.UrlRewriteModule, HttpUrlRewriteToInternal method
            /*
            if (Global.UrlRewriteProvider.ConvertToInternal(e.Url, out obj2))
            {
                if (obj2 is RedirectPageReference) ******************************         
                {
                    HttpContext.Current.Response.Redirect((string) e.Url, true);++++++++++++
                    return;
                }

             */
            //Redirect method returns 302 status code.

            //following code fixes that
            int statusCode = HttpContext.Current.Response.StatusCode;

            if (statusCode == 302)
            {
                //urlrewrite module redirected and page (IHttpHandler) was not executed
                if (HttpContext.Current.Handler == null)
                {
                    PageReference currentPage;
                    string internalUrl = CmsUrlHandler.ConvertToInternalUrl(HttpContext.Current.Request.RawUrl, out currentPage);
                    if (currentPage != null && currentPage != PageReference.EmptyReference && currentPage is RedirectPageReference)
                    {
                        HttpContext.Current.Response.StatusCode = 301;
                    }
                }
            }
        }

#56804
Feb 09, 2012 11:34
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.