0

I have redesigned a website for a client and deleted many useless pages he had. I set up an .htaccess file for him, but after uploading the files I saw that my redirects weren't working. Inside the public_html folder of their server, I see a folder called "aspnet_client", which another thread here on Stackoverflow said is automatically generated on Asp.net 1.1 and below servers.

All of my files end in the .html extension.

So, how do I 301 redirect all of the deleted pages to the current pages on this website? I understand I may have to use a global.asax file.

Could someone show me an example of how to code a global.asax file? I have never done it before. Lets say I wanted to transfer http://example.com/AirConditioning.htm to http://example.com/acrepair.html - what would that code look like?

I apologize if this question is stunningly simple, but I have never worked with an asp.net server, I have only dealt with LAMP servers in the past where .htaccess files would work.

Thank you.

Warnakey
  • 13
  • 5

1 Answers1

0

You can read requested page in global.asax page and perform redirect from here base on some specific condition like case on requested page name.

Another option can be customize and intercept error in order to be able to manage error. http://andynelms.com/2010/08/12/detecting-http-404-errors-in-global-asax/

--

You can start from this article: http://www.codeproject.com/Articles/228879/Global-asax-in-ASP-NET

What you can do is implement only the Application_BeginRequest:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    string currentUrl = HttpContext.Current.Request.Url.ToString().ToLower(); 
    if (currentUrl.EndsWith("myOldPage.aspx"))
    {
        Response.Status = "301 Moved Permanently";
        Response.AddHeader("Location", currentUrl.Replace("myOldPage.aspx", "myNewPage.aspx"));
        Response.End();
    }
}

See also this: Global 301 redirection from domain to www.domain

It's possible to study appropriate replace pattern that use regex for accurate find and replace condition.

--

If you need to handle different file extension, I think that best approach could be: an httpModule:

using System;
using System.Text.RegularExpressions;
using System.Web;

public class RedirectModule : IHttpModule
{
    public RedirectModule()
    {
    }

    public String ModuleName
    {
        get { return "RedirectModule"; }
    }

    void IHttpModule.Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(this.context_BeginRequest);
    }

    private void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication application = (HttpApplication)sender;
        HttpContext context = application.Context;

        //check here context.Request for using request object 
        if (context.Request.FilePath.ToLower().EndsWith("airconditioning.htm"))
        {
            string redirectUrl = "http://www.example.com" + Regex.Replace(context.Request.FilePath, "airconditioning.htm", "acrepair.html", RegexOptions.IgnoreCase);
            context.Response.Redirect(redirectUrl);
        }
    }

    void IHttpModule.Dispose() { }

}

You've to registed this in web.config in differnt way if not integrated pipeline or integrated pipeline

not integrated pipeline:

<system.web>
    <httpModules>
      <add name="RedirectModule" type="RedirectModule"/>
     </httpModules>
  </system.web>

integrated pipeline:

 <system.webServer>
    <modules>
      <add name="RedirectModule" type="RedirectModule"/>
    </modules>
  </system.webServer>
Community
  • 1
  • 1
  • Could you show me an example of how to code a global.asax file? I have never done it before. Lets say I wanted to transfer `http://example.com/AirConditioning.htm` to `http://example.com/acrepair.html` - what would that code look like? – Warnakey May 28 '14 at 17:52
  • Ok, so I have copied and pasted your code into a blank global.asax file (and replaced the 2 "myOldPage.aspx" with "AirConditioning.htm" and "myNewPage.aspx" with "acrepair.html") then placed that global.asax in the wwwroot folder of my server (where index.html is located), but it did not work. Am I doing something wrong still? Do my pages need to be .aspx files? – Warnakey May 28 '14 at 20:04