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.Status = “301 Moved Permanently”;
url = Utility.ResolveUrl(url);
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.