Add Browser Redirection to ASP.NET File

Perhaps a page was created with a spelling error, or a reorganisation of the website means that a particular page has been renamed.

We wish to redirect our visitors to the page to a different address.

In the example below its assumed that the website page has moved to www.example.com/fruit-and-veg/.

We wish to tell the browser to redirect to the new page name by adding a short snippet of code to the page.

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.example.com/fruit-and-veg/");
}
</script>

The code example above can be added to the page. This is for a dynamic asp.net page. It won’t work for a static HTML page.

In the above I’ve included the notification that the change is permanent.

Browsers will cache the redirect, after all you’ve told the browser that you no longer wish the old page to be visited. You may find forcing browsers to adopt a change back difficult to achieve, with the browser reluctant to discover the change. A further permanent redirect back again may be used.

I’ve illustrated the example using a permanent redirect. But, as noted, it’s possible that the browsers will equally permanently cache your rule. If it’s wrong then amending it may well be ignored. Experiment first with a temporary redirect, just to be safe.

If its your intention to redirect more than the single page, perhaps a whole menu section, by adding the redirect into a common header file then you may wish to reconsider your approach. Making use of the file web.config.