How to do a 301 redirect
7 easy ways

By: Einovex
Last Updated: May 9, 2023

Whether you’re a seasoned web developer or just dipping your toes into the world of website management, this comprehensive blog post is your one-stop shop for understanding and implementing 301 redirects like a pro.

Say goodbye to broken links and hello to seamless navigation as we dive deep into the world of server-side redirection. 💫

As for the question of whether redirects are bad for SEO, the answer is no, as long as you use them correctly.

In this guide, I’ll explain how to correctly implement a 301 redirect in a step-by-step manner for non-technical individuals.

Before making any changes, make sure to create a complete backup of your website. This will allow you to revert to a previous state if anything goes wrong during the process.

7 easy ways to do 301 redirect

  1. .htaccess file
  2. php
  3. Node.js
  4. WordPress
  5. Nginx server
  6. ASP.NET
  7. IIS

.htaccess file

If you’re using an Apache server, you can create a 301 redirect by adding a rule to your .htaccess file.

  • Step 1: Locate or create your .htaccess file in your website’s root folder.
  • Step 2: Open the .htaccess file using a text editor.
  • Step 3: Add the following code to the file:
				
					Redirect 301 /old-page.html /new-page.html
				
			

Replace /old-page.html with the original URL and /new-page.html with the new URL you want to redirect to.

Save and close the file. The redirect should now be in place.

php

To create a 301 redirect in PHP, add the following code at the beginning of your old PHP file:

				
					<?php
header("Location: /new-page.php", true, 301);
exit();
?>
				
			

Replace /new-page.php with the new URL you want to redirect to.

Node.js

In a Node.js application, you can create a 301 redirect using the following code:

				
					const http = require('http');

const server = http.createServer((req, res) => {
  if (req.url === '/old-page') {
    res.writeHead(301, { Location: '/new-page' });
    res.end();
  }
});

server.listen(3000);

				
			

Replace /old-page.html with the original URL and /new-page.html with the new URL you want to redirect to.

Nginx server

To create a 301 redirect on an Nginx server, you’ll need to modify your Nginx configuration file.

  • Step 1: Locate and open your Nginx configuration file (usually located at /etc/nginx/nginx.conf or /etc/nginx/sites-available/default)
  • Step 2: Inside the “server” block, add the following code:
				
					location /old-page.html {
  return 301 /new-page.html;
}
				
			

Replace /old-page.html with the original URL and /new-page.html with the new URL you want to redirect to.

Save and close the file. Restart your Nginx server using the command sudo service nginx restart or sudo systemctl restart nginx.

ASP.NET

To create a 301 redirect in ASP.NET, add the following code to your Global.asax.cs file:

				
					protected void Application_BeginRequest(Object sender, EventArgs e)
{
    if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("/old-page"))
    {
        HttpContext.Current.Response.Status = "301 Moved Permanently";
        HttpContext.Current.Response.AddHeader("Location", "/new-page");
    }
}
				
			

Replace /old-page.html with the original URL and /new-page.html with the new URL you want to redirect to.

IIS

For websites hosted on IIS, you can create a 301 redirect using the web.config file.

  • Step 1: Locate or create a web.config file in your website’s root folder.
  • Step 2: Open the web.config file using a text editor.
    Step 3: Add the following code inside the <system.webServer> section:
				
					<rewrite>
  <rules>
    <rule name="Redirect old page to new page" stopProcessing="true">
      <match url="^old-page$" />
      <action type="Redirect" url="/new-page" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>
				
			

Replace /old-page.html with the original URL and /new-page.html with the new URL you want to redirect to.

Save and close the file. The redirect should now be in place.

What's Next

Now that you’ve learned the different methods to implement a 301 redirect for various ways, you can confidently tackle broken links and improve the user experience on your website. 

Correctly implemented 301 (permanent) and 302 (temporary) redirects aid user experience & preserve link equity. However, it’s crucial not to overuse redirects or create redirect chains, as these practices can negatively impact on organic ranking, website’s performance and SEO.

As a next step, it’s essential to monitor your website’s performance and ensure that the redirects are functioning as intended. In addition, keep an eye on your website’s analytics to observe any changes in traffic patterns or user behavior after implementing redirects.

Table of Contents
More Technical SEO Guide