Change status code 302 to 301 Permanent for Config.DefaultRedirectPath

Is there a way to change the Config.DefaultRedirectPath status code from using 302 Temporary Redirect to 301 Permanent Redirect?

Config.DefaultRedirectPath = "/default";

You can register a IAppHost.RawHttpHandlers which gets executed before any request to return a permanent redirect when it matches any pathinfo you want to redirect, e.g:

RawHttpHandlers.Add(httpReq =>
    httpReq.PathInfo == "/anypath"
        ? new RedirectHttpHandler { 
               StatusCode = HttpStatusCode.MovedPermanently,
               RelativeUrl = Config.DefaultRedirectPath }
        : null);

Otherwise I’ve just made the HttpHandlerFactory handlers public in this commit which will let you modify the default handlers to get the behavior you’re after and replace the Status Code of the default redirect handlers with:

AfterInitCallbacks.Add(host => {
    if (HttpHandlerFactory.DefaultHttpHandler is Host.Handlers.RedirectHttpHandler redirect)
        redirect.StatusCode = HttpStatusCode.MovedPermanently;
    if (HttpHandlerFactory.NonRootModeDefaultHttpHandler is Host.Handlers.RedirectHttpHandler redirect)
        redirect.StatusCode = HttpStatusCode.MovedPermanently;
});

This change is available from v5.4.1 that’s now on MyGet.

1 Like

Thanks that’s helpful!

Not to be needy and I didn’t look to see what all is involved to make the change, but I was hoping for something like:

Config.DefaultRedirectStatusCode = HttpStatusCode.MovedPermanently

Only because it seems more clear and it’s the only thing that would be modified. It would happen on every website we create. The reason for the 301 is that it is more SEO appropriate than 302.

At least according to some sources like

It’s already configurable, not going to pollute the configuration with switches/functionality that just adds additional choices/complexity that will rarely ever be used. If you intend to use it a lot wrap it in a custom extension method that your AppHost’s calls instead.

If you use MovedPermanently you’ll end up losing control of where to redirect requests as browsers will stop requesting where to go and instead go directly to the previously redirected resource.

Sounds good! I’m good with that.