301 Redirect: The Complete Guide

One of the most important aspects of building and managing websites is routing and redirects, especially 301 redirects. A 301 redirect is a method that aims to manage bounces from one URL to another in an efficient and optimized way with tuning. This action is important for maintaining the site’s relevance and improving SEO (search engine optimization).

What is a 301 redirect?

An HTTP tool that specifies a “permanent” redirect from an origin URL to a destination URL. Simply put, it means that the original page has been deleted or moved to another address, and the information is now at the new address.

why is it important?

  • Maintaining SEO elements : When you change a URL, the referral will adjust Google and other search engines to the new address, so that all the accumulated “juice” (external links, rankings, etc.) will be transferred to the new address.
  • Improving the user experience : The referral saves users time and reduces the chance that they will encounter wrong pages or path problems.

How to create 301 redirects?

 

An example of a 301 redirect in HTML combined with JAVASCRIPT

In the HTML language itself it is not possible to make a 301 redirect, since it is not a programming language and therefore cannot control the HTTP response codes. But you can use JavaScript to redirect the user to another URL. It should be noted that such a reference will not constitute a correct reference in terms of SEO, but will do the job in terms of the user.

<!DOCTYPE html>
<html>
<head>
    <title>Old Page</title>
    <script type="text/javascript">
        // הפנייה לדף החדש
        window.location.href = "http://www.example.com/new-page.html";
    </script>
</head>
<body>

<!-- תוכן הדף, אם יש (לא יוצג בדרך כלל מכיוון שהפנייה תתבצע מיד) -->

</body>
</html>

  This example uses JavaScript to redirect the user to a new page. As mentioned, this is no substitute for a real 301 redirect when it comes to proper SEO management. For this, it is better to use the tools detailed in the previous part of the article. An example of a PHP page

A site’s home page can automatically redirect users to the site’s new page. Here is a simple example in PHP:

<?php
// הפניית 301 לעמוד הבית החדש
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.example.com/new-homepage.html');
exit();
?>

 

Using .htaccess on Apache servers

Open the file .htaccess at the root of the website and add the following line:

Redirect 301 /old-page.html /new-page.html

Using Nginx

Open the Nginx configuration file and add the line:

location ~ ^/old-page.html$ {
    return 301 /new-page.html;
}

 

Summary

The referral is a powerful and flexible tool used to route users and search engines to the correct address, while maintaining SEO elements and improving the user experience. Any good project should consider using this type of references during the design and construction of the website.