Friday, 12 February 2016

HTML / ASP.NET / Javascript - 301 Redirect

There are multiple ways to implement a 301 Redirect on a webpage.
Below are methods to do redirection as soon as the page gets the visit using HTML Meta tag, ASP.NET and Javascript.
The most preferred way is ASP.NET than HTML Meta tag and Javascript as it is the most compatible option.

<script language="C#" runat="server">
// ASP.net permanent URL redirect
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://yahoo.com");
Response.End();
}
</script>
<!DOCTYPE html>
<html>
<head>
<!-- HTML meta URL redirect -->
<meta http-equiv="refresh" content="0; url=http://yahoo.com">
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
// Javascript URL redirection - generated by www.rapidtables.com
window.location.replace("http://yahoo.com");
</script>
</body>
</html>
The above sample will redirect user to http://yahoo.com as soon as he reaches this page.