Simple 301 Redirects ASP.NET 4.0 in Code

Sometimes it is just easier to code in 301 redirects into an application that you are building. Specially if it is replacing a system on the same domain.

I had this recently with https://carquote.quoteline.ie.

It is very easy to use your Global.asax file to program in 301 redirects without having to use IIS when using ASP.NET 4.0.

void Application_BeginRequest(object sender, EventArgs e)

    {

        // old quoteline system 301 redirects

        string localUrl = Request.Url.LocalPath;

        ArrayList redirectUrls = new ArrayList();

        redirectUrls.Add(“motor1.aspx”);

        redirectUrls.Add(“motor2.aspx”);

        redirectUrls.Add(“finalQuoteAll.aspx”);

        redirectUrls.Add(“QuoteAssumptions.aspx”);

        foreach (string url in redirectUrls)

        {

            if (localUrl.IndexOf(url, StringComparison.OrdinalIgnoreCase) >= 0)

            {

                HttpContext.Current.Response.RedirectPermanent(“https://carquote.quoteline.ie/”);

            }     

        }  

    }

If you have more complex URLs that contain query string data that must be maintained @improvedk has a fairly good article about that… review the resource link.

Resource:

Advertisements

Published by

Alan Feekery

Developer, Gamer, Musician, Cyclist and big Motorsport fan... enjoys the odd cup of coffee :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s