Redirect301

For SEO purposes I needed to make a 301 permanently moved especially for the double inserted URLs. Since the Response.Redirect of the .net framework makes a redirect with a 302 temporary redirect, I thought it would be very useful to have a Redirect301 at my service.

For this purpose I wrote a .net extension method on HttpResponse class. (for more about extensions visit this blog) Extensions can be pretty useful for small tasks where it is not worth to derive a new class from the older one. So, it is just a static method, with a slight difference. You’ll notice it in the first parameter. Thanks to the this keyword, we will be able to use this method on any instance of HttpResponse class.

public static void Redirect301(this HttpResponse response, string url)
{
response.Clear();
response.Status = “301 Moved Permanently”;
url = Utility.ResolveUrl(url);
response.AddHeader(“Location”, url);
response.End();
}

But wait!! If you are out of the page or control, there isn’t a easy way to access ResolveUrl. -Maybe breating a temporary page in memory and then calling its ResolveUrl could have been done, but it would be a very indirect approach. After googling for a few minutes I found ResolveUrl() without Page article came to my help. That was exactly what I was looking for. So in my Utilities class, I added the ResolveUrl() and that’s all.

So from now on, when I need I can use Reponse.Redirect301(“~/SomeUrl”) exactly like Response.Redirect .

P.S. I’m terribly new to wordpress, so I didn’t have time to reformat the code section, I’ll do it ASAP.

Annunci

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger hanno fatto clic su Mi Piace per questo: