Performing 301 Redirect with Apache, IIS, PHP, and others

Apache

If you use Apache with the mod-rewrite module installed, 301 Redirect may be set up by editing your .htaccess file found at the root directory of your website. The following is a sample of what you might add to your .htaccess file; in this example, we are redirecting any visits to /blah/pictures.html to its new location, http://ww2db.com/photo.php.

Redirect 301 /blah/pictures.html http://ww2db.com/photo.php

Sometimes you may encounter a situation where you changed technology of your platform, for example, from static HTML pages to dynamic PHP pages. With the sample code below, you can redirect all HTML pages to PHP pages by the same name.

RedirectMatch 301 (.*).html$ http://ww2db.com$1.php

Looking to use .htaccess to redirect an entire site because you have moved from one domain to another? This is what you want to do:

Redirect 301 / http://ww2db.com/

PHP

If your site’s pages are made of PHP files, redirection is fairly easy. If you had moved the /old/pictures.php page to the new location of http://ww2db.com/photo.php, modify the /old/pictures.php page by adding the following PHP snipped on the top of the code, ie. before any HTML code is written to screen.

header("HTTP/1.1 301 Moved Permanently");
header("Location: http://ww2db.com/photo.php");

IIS

If you using Microsoft’s Internet Information Services (IIS), perform the following steps to do a 301 Redirect.

  • Right click on “My Computer”, go to “Manage”, “Services and Applications”, and then “Internet Information Services (IIS) Manager”.
  • Under “Web Sites”, look for the particular folder or file you wish to redirect. Right click and select “Properties”.
  • Click the “A redirection to a URL” radio button. In the bottom section that had just appeared, and enter the new URL in the “Redirect to:” text box, and make sure to check “A permanent redirection for this resource” checkbox.
  • Click OK to complete the process.

ASP and ASP.NET

Classic Active Server Pages (ASP) web pages can be redirected in a method very similar to PHP above.

Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://ww2db.com/photo.php"

ASP.NET redirect code is very similar to its ASP predecessor’s.

private void Page_Load(object sender, System.EventArgs e) {
  Response.Status="301 Moved Permanently";
  Response.AddHeader("Location","http://ww2db.com/photo.php");
}

JSP

Redirection for Java Server Pages (JSP), again, is similar to the other scripting languages we have seen above.

response.setStatus(301);
response.setHeader("Location","http://ww2db.com/photo.php");
response.setHeader("Connection", "close");

CGI/Perl

If you are using Perl-based CGI code, this is the code you may wish to deploy to perform a 301 Redirect.

$q=new CGI;
print $q->redirect("http://ww2db.com/photo.php");

Cold Fusion

ColdFusion pages can be redirect also by working with the header.

<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="http://ww2db.com/photo.php">

Ruby on Rails

Not surprisingly, Ruby on Rails pages are redirected in a very similar manner.

def old_action
headers["Status"]="301 Moved Permanently"
redirect_to "http://ww2db.com/photo.php"
end

How to output a string in PHP

In PHP, to output a line, we can use either the echo() or print() functions. All six examples below have the same output of “hello world”:

echo('hello world');
print('hello world');
print 'hello world';

echo("hello world");
print("hello world");
print "hello world";

Note the print() function can work without parenthesis. That is because print() is not really a function; print() is actually a language construct. Functions print() and echo() pretty much do the same thing, so generally you can use whichever one you prefer, though echo() actually gets processed faster, so if you are working with a larger application, this may be something to keep in mind.

To output a string variable, we can do something like this:

$name = "world";

echo('hello '.$name);
print('hello '.$name);
print 'hello '.$name;

echo("hello ".$name);
print("hello ".$name);
print "hello ".$name;

echo("hello $name");
print("hello $name");
print "hello $name";

Once again, all examples above output “hello world”, and the character used to concatenate the “hello” and the variable that contains “world” is the period character; the period is equivalent to the ampersand sign in VB/ASP and the plus sign in Java/JSP. Now, see the third set above where we used double quotes to enclose the string and the variable together. This is a special functionality you can use only with double quotes. It is a way to make your coding process slightly simpler, and the resulting code slightly easier to read, but keep in mind that this method makes the code run just a tad bit slower because when the PHP engine sees the double quote, it needs to be prepared to find and process variables — even if none are found.

In conclusion, echo() and print() are nearly identical, and the usage of double and single quotes are nearly identical, and you may choose whichever one that suits your style better. In terms of optimizing processing speed, however trivial, using echo() with single quote is preferred.