How do I create a 301 redirect?

With a 301 Redirect, a webserver returns the HTTP Status Code 301 to the requesting web client. which could be Google-Bot, for example, or a users browser. 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.

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.

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:

header("HTTP/1.1 301 Moved Permanently");
header("Connection: close");

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

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  [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 “”. 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

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 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.


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: