301 Permanent Redirect Tutorial by Brian V. Bonini






301 Permanent Redirect by Brian V. Bonini



New domain name?

Need to change a file name?

Hierarchy of your servers directory structure change?


meta http-equiv="refresh"... is highly frowned on by search engines and is commonly used by spammers. As such, THIS SHOULD BE AVOIDED.

A 301 Redirect as it is commonly referred to will allow you to make these changes without compromising your hard earned SEO results.

Using the Apache web server, fortunately this is a simply task.

There are a few different places you can set various 'Redirect' directives such as your servers main configuration file (typically httpd.conf) or within a 'Virtual Host' container inside one of your server configuration files. The final method and the one we will be discussing here is using your servers directory Auth file (AKA: .htaccess).

The first thing you do is create a file named .htaccess

There are a couple different way to approach this but in it most simple form you can just issue a 'Redirect' request in your .htaccess file like so:

Redirect /foo http://foobar.com/foo

In this example if the client requests
http://myserver.com/foo/foobar.txt,

it will be told to access
http://foobar.com/foo/foobar.txt instead.

Now this is not quite complete yet, since we need to specifically send a 301 status code. Without the status argument "Redirect" will send a temporary redirect status (302). So we simply take the above example and change it to:

Redirect 301 /foo http://foobar.com/foo

OR

Redirect permanent /foo http://foobar.com/foo

And there is also a specific 'RedirectPermanent' directive as well so you could just as easily say:

RedirectPermanent /foo http://foobar.com/foo

So far so good? Excellent. So now you ask what if I want to change ALL my .htm files to .html. Introducing 'RedirectMatch' which makes use of standard regular expression syntax.

RedirectMatch (.*)\.htm$ http://myserver.com$1.html

Going into an in depth explanation of regular expression if beyond the scope of this tutorial but a quick Google search for "Perl compatible regular expression syntax" should set you on your way.

What the above example does is match any character '.' any number of times '*', i.e., that is it will match everything. (.*) the parenthesis group the result into a variable which we will use with a back-reference ($1) later in the directive. Next, '\' simply escapes the following '.'. Since '.' has special meaning in regular expression syntax we need to escape it if we want to use it in the literal sense, '\' provides the escape sequence. Next is the '.htm' which we know is a static part of the search string we're looking for followed by '$' which simply marks the end of the pattern in a regular expression (regex). So the above example takes any file with an .htm extension and redirects the client to the same file at the location specified, i.e., http://myserver.com$1.html

But wait, what's that '$1'? Well remember we said we were going to store a variable for use in a back reference later, well here it is. The '$1' simply says that whatever was in (.*) is now represented by $1

So, the client request for example foobar.htm, the above directive matches this and redirects the client to http://myserver.com/foobar.html because 'foobar' is stored in '$1'. Just as with 'Redirect', 'RedirectMatch' takes a status argument so to issue a 301 (permanent) redirect we do this:

RedirectMatch 301 (.*)\.htm$ http://myserver.com$1.html

Another method we can use is via mod_rewrite. This requires that the mod_rewrite module is active on your webserver. It usually is and is done by the system administrators when they installed the webserver. mod_rewrite is a very powerful URL re-writing engine and we will only by scratching a hair on its head here.

Again, in your .htaccess file

RewriteEngine ON
RewriteRule ^(.*)$ http://mynewdomain.com/$1 [R=301,L]

The above example will re-map your old domain to a new one and issue a 301 status code (permanent redirect). So a request for

http://olddomain.com/foobar.html will go to

http://mynewdomain.com/foobar.html

If you simply want to redirect all requests regardless of the page requested to the new domain you could use:

RewriteRule /.* http://mynewdomain.com/ [R=301,L]

In this case no matter what file or directory is requested they will all go to

http://mynewdomain.com/ i.e., http://myolddomain.com/foobar.html

will go to http://mynewdomain.com/

The [R=301,L] means redirect the client and send a 301 status code (R=301) and make this the last rule (L).

Whichever method you decide on, once completed upload the resulting file to your webservers document root and you should be on your way.


Related 301 Redirect and Resources

Also, a Google search for '301 redirect+seo' or 'mod_rewrite+301 redirect' should turn up tons of information.



Copyright © 2005 by Brian V. Bonini All Rights Reserved

 

Brian V Bonini

Problem Solver



Website Development Training Specialist Brian  V. Bonini


Since 1997 (after a brief stint as a computer hardware specialist) I've been the executive producer and founder of gfx-Design and have more recently added ONI Data Solutions to the roster. I oversee all aspects of design, development, integration and quality control in addition to hands on development of all phases of our projects.

 

I am well versed in past, present and future W3c (X)HTML/XML, XSL/XSLT, DOM, and CSS specifications. I've built many web applications using LAMP technologies (Linux, Apache, PHP, MySQL) and specialize in configuring and working in each of these technologies individually as well. I'm an avid proponent of web standards, accessibility, open source and anything anti-Microsoft. As such, I've been an exclusive Linux/UNIX user for almost 10 years now and enjoy administering these systems. With the birth of ONI Data Solutions we are now venturing out into a more broad range of data related services. One of which will be supplemental support services for individuals and companies not receiving the level of support they need from their primary providers. In addition we provide a full range of web hosting solutions, domain registration and management, application installation and configuration.


In this classroom I provide basic advice and solutions for some of the more intricate areas of webdesign/development and server administration. Including Apache's mod_rewrite for mapping dynamic to static URLs for optimum SEO performance, application installation and configuration, application programming and while we have two excellent HTML and CSS tutors you will often find me chiming in with my $0.02 there as well.

 

In general, I love anything mentally challenging. A problem that needs solving (of any nature) is what truly motivates my work ethic.

 

Professional memberships:
  • HTML Writers Guild
  • International Webmasters Association
  • International High IQ Society

On a personal note: I am a competitive athlete (cycling) and as such, am very interested in sports physiology, psychology and nutrition. When I'm not at the helm of a cathode ray tube I'm on my bike, in the gym, or teaching aerobics (spin) classes. I love pushing myself physically to the limits to see how much I can endure. I'm married for 14 years now to Laura who is currently completing her Masters work and have a 13 year old daughter Hayley.


Contact Website Development Training Specialist Brian V. Bonini





Free Online Training Classes


What began as one small SEO training class, has grown to three active groups. These classes provide structured training, to help you develop a successful online business.

  • SEO Training Class

    The online classes provide structured training on the basics of ethical search engine optimization, designed to increase your visibility in the search engines.

  • Successful Website Marketing Training
    Lessons include developing website credibility, internet marketing strategies, developing strong web writing skills, and increasing targeted website traffic.

  • Website Development Training Class
    This classroom provides website development training with a special focus on small business owners. The current lessons include, learning the basics of HTML, CSS, Internet Security and other website development techniques.





Company


Web Design

Graphics

Web Services

Articles

Training Center

Resources

About Us
Contact Info
Award Pages
Web Portfolio
Payment Info

Web Design
Website Cost
Small Business
Motorcycle Sites
Brochure Website

Biker Graphics
Graphic Design
Custom Buttons
Website Banners
Custom Graphics

SEO Consulting
Website Review
Business Planning
Internet Marketing
Stock Photography

Computer
Marketing
Motivational
SEO Articles
Web Design

SEO Training
SEO Techniques
Website Marketing
Web Development
Class Registration

Site Map
Link To Us
Submit Site
Website Tools
1and1 Hosting