Search the Site


bottom corner

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

When you change the URL of a page or move an entire site to another URL, all those sites that link to you may get broken links, and if you care about SEO results, all the search engine "points" you have been building over the years might be all lost. By employing the 301 Redirect technique, you might be able to avoid these problems completely.

This page is filed under keyword(s): seo, apache, php, iis, asp,, jsp, perl, coldfusion, rubyonrails.


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,

Redirect 301 /blah/pictures.html

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$$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 /


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, 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");


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.


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",""

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";


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

response.setHeader("Connection", "close");


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("");

Cold Fusion

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

<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="">

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

Did you find this page useful? Please consider browsing other articles or subscribing to the RSS feed to keep up with latest.

This page is filed under keyword(s): seo, apache, php, iis, asp,, jsp, perl, coldfusion, rubyonrails.
Author: C. Peter Chen
Last updated: 23 Sep 2008

bottom corner