Ricky
Ricky

Reputation: 2907

IIS - How to redirect

How can I redirect www.example.com/foo to www.example.com\subdir\bar.aspx in IIS?

Note: a file called "foo" does not exist. I'd like to just redirect a URL with that pattern to the second URL

Upvotes: 1

Views: 47

Answers (1)

mason
mason

Reputation: 32693

The best thing to do is not redirect, but instead use routing to map the URL to the web form. Then you keep a nice clean URL which is easy for users to type, and it looks better for search engines. We can accomplish that with the MapPageRoute method.

Add this to your Global application class (global.asax or global.asax.cs)

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes);
}

void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("", "foo", "~/subdir/bar.aspx");
}

Alternatively, you can add this to your web.config to do a redirect.

<configuration>
  <location path="foo">
    <system.webServer>
      <httpRedirect enabled="true" destination="/subdir/bar.aspx" httpResponseStatus="Permanent" />
    </system.webServer>
  </location>
</configuration>

Upvotes: 1

Related Questions