How do I create a 301 redirect?

With a 301 Redirect, a webserver returns the HTTP Status Code 301 to the requesting web client (browser) which could be Google-Bot, for example, or a user. This status code tells the client that the requested resource (a URL for example) has permanently moved and is now available on a new URL. It is good practice to keep pages redirected if their URL changes for any reason.

You can easily set up a 301 redirect in PHP or through the .htaccess file and the necessary mod_rewrite module, if you use an Apache Webserver.

If you use WordPress and have no access to server files, it’s possible to install plugins that can manage and perform redirects for you. Information on an example WordPress redirection plugin is shown below.

Creating a 301 redirect through .htaccess

If you want to create a 301 redirect through the .htaccess file you need an Apache webserver and an activated mod_rewrite module. Considering that Apache is one of the most popular webservers, almost all big webhosts will use it. In addition, the mod_rewrite module is already activated in most cases. The .htaccess-file is also included per default in most webhosting bundles.

The .htaccess file is found in the top-level directory of the web content but can also work within a subdirectory.

Paste the following source code into the .htaccess-file:

RewriteEngine On
RewriteRule  ^/directory/a-document.html
https://www.domain.com/a-document.html  [R=301]

Function: By using the command “RewriteEnginge On” the mod_rewrite module of the Apache webserver will be activated. The second line of code starts with “Redirect 301” and defines the HTTP status code that is supposed to be returned. Next, specify the path of the document that is going to be redirected – such as “https://www.domain.com/directory/a-document.html”. Afterwards add a space and then type in the target URL onto the same line – the target URL is the URL you want to redirect the Google-Bot as well as the users to.

In our example, the file “a-document.html” cannot be found at “/directory/” anymore, but one level higher up, at the domain root.

Note that there are a number of redirect rules and syntax that can be used. One example is the permanent redirection of http to https.

To permanently redirect http requests to https requests it is possible to intercept them using a port 80 redirection. Port 80 is the well know TCP port for non-encrypted traffic.

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]

Function: By capturing all requests arriving on the server port 80 they can be permanently redirected, with the correct and full URL to the https equivalent. 

For more information, read the introduction to the Apache mod_rewrite syntax and rules at the apache website.

Creating a 301 redirect in PHP

If you want to create a 301 redirect in PHP the source document has to be a PHP file. If this is the case, the PHP code for the 301 redirect can be added directly into the document that should be redirected.

Example: Paste the following source code in the first line of the document you want to redirect:

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://www.domain.com/the-new-name.php");
header("Connection: close");
?>

Function: By using the above source code in the file “the-old-name.php”, which can be reached at “domain.com/the-old-name.php”, all visitors will now be redirected to the new source “domain.com/the-new-name.php“ (line 3) and the HTTP Status Code 301 moved permanently will be returned.

Creating a 301 redirect with NGINX, Lighttpd or Microsoft Internet Information Services (IIS)

Creating a 301 redirect on other webservers, like NGINX, Lighttpd or the IIS, is not as trivial for inexperienced users.

Creating a redirection via a WordPress plugin

As with any plugin one must remember these things.

  • You introduce more code to your website. This can increase delays and decrease security
  • If a plugin development is hacked, this code might reach your server
  • Plugins may change ownership, which may change the future stability or support for your plugin
  • If the development stops, the plugin may go out of date and you’re left with a migration problem to avoid future security or compatibility problems
  • If you remove the plugin, all previous settings and entries may be lost

One WordPress plugin to consider is redirection.me which is a mature and stable plugin used by many people. It’s free, there’s no paid version and it provides a simple interface with which one can manage redirects.

redirection.me user interface as seen in WordPress

Is there a limit to how many 301 (Permanent) redirects I can do on a site?

Is there a limit to how many 301 (Permanent) redirects I can do on a site? How about how many redirects I can chain together?

Additional Information about this topic:

Steve Paine
28.11.2022