← All articles

Redirect Code: How to Use it Properly

This blog post aims to provide an in-depth understanding of the 301 and 302 redirect codes and demonstrate their implementation in various programming languages, including .htaccess, PHP, JavaScript, HTML, Java, Python and C#.

Understanding 301 and 302 Redirect Codes

301 and 302 redirect codes are HTTP status codes that indicate a URL has been moved to a new location, either permanently or temporarily. The primary difference between the two is the duration of the redirect:

  • 301 Permanent Redirect: A 301 redirect informs search engines and browsers that a URL has been permanently moved to a new location. This results in the transfer of link equity and search engine rankings to the new URL.
  • 302 Temporary Redirect: A 302 redirect indicates that a URL has been temporarily moved, and search engines should continue to index the original URL. It is typically used for temporary URL changes, such as during website maintenance or A/B testing.

Implementing 301 and 302 Redirects in Various Languages

Let's explore the implementation of 301 and 302 redirects in different programming languages.

.htaccess

These are examples of redirect rules in the .htaccess file that redirect traffic from a specific page to a new page on the same website. The first rule performs a 301 permanent redirect, while the second rule performs a 302 temporary redirect.


# 301 Redirect
Redirect permanent "/this-page.html" "/to-this-page.html"

# 302 Redirect
Redirect "/this-page.html" "/to-this-page.html"

PHP

In PHP, 301 and 302 redirects can be achieved using the header() function:

// 301 Redirect
header('Location: /new-page.php', true, 301);
exit();

// 302 Redirect
header('Location: /new-page.php', true, 302);
exit();

JavaScript

In JavaScript, 301 and 302 redirects can be implemented using the window.location object:

// 301 and 302 Redirect
window.location.replace('/new-page.html');

Note that JavaScript doesn't differentiate between 301 and 302 redirects. Instead, search engines usually consider JavaScript-based redirects as temporary (302).

HTML

HTML-based 301 and 302 redirects can be achieved using the meta refresh tag:

<!-- 301 and 302 Redirect -->
<meta http-equiv="refresh" content="0; url=/new-page.html">

Similar to JavaScript, HTML doesn't differentiate between 301 and 302 redirects, and search engines typically treat them as temporary (302).

Java

In Java, you can implement 301 and 302 redirects using HttpServletResponse:

// 301 Redirect
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", "/new-page.jsp");

// 302 Redirect
response.sendRedirect("/new-page.jsp");

Python (using Flask)

In Python, using the Flask web framework, you can implement 301 and 302 redirects with the redirect() function:

from flask import Flask, redirect, url_for
app = Flask(__name__)

# 301 Redirect
@app.route('/old-page')
def old_page():
    return redirect('/new-page', code=301)

# 302 Redirect
@app.route('/temporary-page')
def temporary_page():
    return redirect('/new-page', code=302)

C# (using ASP.NET Core)

In C#, using the ASP.NET Core framework, you can implement 301 and 302 redirects using the RedirectToAction method:


using Microsoft.AspNetCore.Mvc;
public class RedirectController : Controller
{
	// 301 Redirect
	public IActionResult OldPage()
	{
		return RedirectToActionPermanent("NewPage");
	}

	// 302 Redirect
	public IActionResult TemporaryPage()
	{
	    return RedirectToAction("NewPage");
	}

	public IActionResult NewPage()
	{
	    return View();
	}
}

Conclusion

Understanding the differences between 301 and 302 redirect codes and knowing how to implement them in various programming languages is essential for effective website management. By following this comprehensive guide, you can efficiently utilize these redirects to maintain website integrity, improve user experience, and optimize search engine performance.