301 Redirects: IIS, Apache, Nginx and Drupal

One of the most important things an SEO should do is have control over the 301 redirects.

301 passes the page rank from an existant or cached url to another one it they're SEO friendly.

Depending on the webserver your're using there are different ways to do it:

 

301 IIS Redirects

Using the console, copy IsapiRewrite4.dll and IsapiRewrite4.ini to anywhere on your inetpub folder.

Edit IsapiRewrite4.ini and add the redirects:

RewriteRule ^/index.php$ / [R=301]

RewriteRule ^/old-url$ /valid-url [R=301]

on the console, IIS Manager>WebSites> You must add the ISAPI filter you've just created

If you don't have access to you console (like on a IIS godaddy shared hosting), you have to create a directory called the same way your file and add a default.asp file inside it redirecting to the proper location.

Example:

existing file : /products.html

new url: /products.php

Rename products.html to products.php, create a directory called /products.html/ add a file called /products.html/default.asp or .aspx with the content of the proper redirect (read below)

aspx 301 redirect

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.url.com");
}
</script>

asp 301 redirect

<%
   Response.Status = "301 Moved Permanently"
   Response.AddHeader "Location", "/new-url.html"
%>

Apache 301 Redirects (only on linux/ unix servers)

301 Redirect from the non www to the www version

Create a .htaccess file on the directory and add the folowing:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.url\.com$ [NC]
RewriteRule ^(.*)$ http://www.url.com/$1 [L,R=301]

apache 301 Redirect a single url

Create a .htaccess file on the directory and add the folowing:

RewriteEngine On
Redirect /old-directory/old-file.html http://www.url.com/new-directory/new-file.html

php 301 Redirects

Create a php file with the folowing content:

<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.url.com" );
?>

Nginx 301 Redirect

using the sites-enabled file configuration file (/etc/nging/sites/sites-enabled)

Add the filter rule and the rewrite rule:

 location ^~ /existing-url-path {

 if (!-e $request_filename) {
                rewrite  ^/(.*)$  /new-url?parameters=$1  last;
                break;
            }

}

Drupal 301 Redirects

Enable the global_redirect module, then add the redirect using the menu (site building > url redirects > add)