With a 301 Redirection, a webserver returns the HTTP Status Code 301 to the requesting client, 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.
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
- Creating a 301 redirect through .htaccess
- Creating a 301 redirect with NGINX, Lighttpd or Microsoft Internet Information Services (IIS)
- Video Explanation By Matt Cutts / Google on this topic
- Additional Information about this topic:
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 condition is met, the PHP code for the 301 redirect will be added directly into the document that is supposed to be redirected.
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: http://www.domain.com/the-new-name.php"); header("Connection: close"); ?>
Function: By pasting the above source code into 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 through .htaccess
If you want to create a 301 redirect through the .htaccess-file you need an Apache webserver and activate the mod_rewrite module. Considering that Apache is one of the most popular webservers, almost all big webhosters will use it. In addition, the needed mod_rewrite module is already activated in most cases. The .htaccess-file is also included per default in most webhosting bundles.
Paste the following source code into the .htaccess-file:
RewriteEngine On Redirect 301 /directory/a-document.html http://www.domain.com/a-document.html
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, you specify the path of the document that is going to be redirected – such as “http://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.
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.
Video Explanation By Matt Cutts / Google on this topic
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?