Red Antigua Logo
Yet another piece of web.
Search this site (by Google)
Tools    (top)
Your IP
Check a site for broken links
(W3C)

Perl modules    (top)
Tree::Numbered::Tools
(CPAN)
Perl tutorials    (top)
Perl modules
HTML::Template
CGI::Application
Cookies with CGI::Application
Upload files with CGI::Application
Download files with CGI::Application
Redirect with CGI::Application
CPAN shell
Install DBD::mysql from the CPAN shell
Perl trim function
Validate an IP with Perl
Run suid Perl scripts under Apache
Perl taint mode
Perl date functions with Date::Calc

In Spanish
Curso de Perl

C tutorials    (top)
C - Introduction
C - Absolute beginner's Emacs
C - Examples for beginners
C - Makefile examples
C - Autotools examples
Server configurations    (top)
DNS
Apache
Apache Authentication and Access Control
mod_perl on FreeBSD
MySQL
MySQL add account
phpMyAdmin
Squid
DHCP

UNIX on Windows    (top)
MSYS2 - UNIX environment for MS Windows 32/64 bits
Apache setup on Windows
MySQL setup on Windows
PHP setup on Windows
Perl setup on Windows
Emacs setup on Windows
PuTTY
WinSCP
GIMP on Windows
MinGW - gcc on Windows
MSYS - UNIX-styled shell on Windows
msysDTK - autotools on Windows
GDB for MinGW on Windows

Misc. FreeBSD/UNIX    (top)
'portupgrade' on FreeBSD
'ipf' on FreeBSD
'pf' on FreeBSD
'su' on FreeBSD
Mount an ISO image under FreeBSD
Load the correct sound driver under FreeBSD without knowing what sound card you are using
Simultaneous sound channels on FreeBSD
FreeBSD network stuff
DOS-to-UNIX file conversion
favicon.ico on UNIX
Emacs tips
Command Line Calculator
Save multimedia streams with 'mplayer'
xargs - solution to 'Argument list too long'
Process multiple images from the command line using 'ImageMagick'
Turn the system bell off under X Windows
Process each line in an input file from the command line (or in a shell script)
How to keep a program running in the background using 'nohup'
How to remove symbolic links in the current directory using 'find' and 'rm'
How to remove Emacs backup files in the current directory and all subdirectories using 'find' and 'rm'
How to execute .profile without logging in
Configure X to handle non-English characters
How to move /var to /usr/var

Redirect a web page    (top)
Redirect to another web page
Apache redirect
C redirect
Perl redirect
PHP redirect
HTML redirect
JavaScript redirect

Javascript    (top)
Trim function
Login form
Register form
Popup window

Redirect a web page
Redirect a web page        
Apache redirect
C redirect
Perl redirect
PHP redirect
HTML redirect
JavaScript redirect

Redirecting a web page can be done in many ways, languages, and sides (both client and server).
Generally, it's better to redirect a web page on the server side, as you will not depend on the (to you unknown) browser.
Using language such as C, Perl, PHP, or JavaScript permits you to make a redirection conditional.
Using Apache permits you to redirect one or more pages using one single regular expressions.
That is, There Is Always More Than One Way To Do It.

The following note applies to all redirect methods, both client and server (taken from the Perl's CGI man page):
       One hint I can offer is that relative links may not work
       correctly when you generate a redirection to another
       document on your site.  This is due to a well-intentioned
       optimization that some servers use.  The solution to this
       is to use the full URL (including the http: part) of the
       document you are redirecting to.
(A relative redirection works on Apache, but you get a warning message in your log.)


Apache redirect (top)

Apache redirects are recommended if you want to redirect all web pages in a directory to a new place.
In httpd.conf:
Redirect /mydirectory http://www.redantigua.com/personal
For example, accessing http://www.myserver.com/mydirectory/photos.html will redirect the browser to http://www.redantigua.com/personal/photos.html.

The following example redirects any page (even non-existing ones) to a new site:
RedirectMatch (.*) http://www.redantigua.com
This example makes the browser to load the web page from this server, but using another server to load the images:
RedirectMatch (.*)\.png$ http://www.imageserver.com/images/$1.png
RedirectMatch (.*)\.jpg$ http://www.imageserver.com/images/$1.jpg
RedirectMatch (.*)\.jpeg$ http://www.imageserver.com/images/$1.jpeg
RedirectMatch (.*)\.gif$ http://www.imageserver.com/images/$1.gif
C redirect (top)

To redirect a web page using C, first be sure that the execution of CGI scripts is permitted in the directory where you want to put the redirect page. On Apache, use the ExecCGI option.
The directive
AddHandler cgi-script .cgi
must also be configured (assuming that you use .cgi as the extension for your redirect script).

The C source code file redirect.c:
#include <stdio.h>
main( ) {
  printf("Status: 302 Moved\r\nLocation: http://www.redantigua.com\r\n\r\n");
}
Compile as (assuming that the C source code is in the same directory as where you want to put the executable 'index.cgi'):
cc -o index.cgi redirect.c
Make it executable
chmod 755 index.cgi

Perl redirect (top)

No matter if a CGI script is written in C, Perl, or whatever language, execution of CGI scripts must be enabled./> So the requirements mentioned above (the ExecCGI option, the AddHandler cgi-script .cgi directive) also aplly for Perl CGI scripts./>
The easy-to-remember way:
#!/usr/bin/perl -w
use strict;
use CGI qw/:standard/;
print redirect('http://www.redantigua.com');
More efficient, not loading the CGI module:
#!/usr/bin/perl -w
use strict;
print 'Status: 302 Moved', "\r\n", 'Location: http://www.redantigua.com', "\r\n\r\n";
Using the CGI::Application framework:/>
Click here for a tutorial how to redirect using CGI::Application.
/> PHP redirect (top)
<?php
header("Location: http://www.redantigua.com/");
?>
If you name the above script index.php, to redirect a directory, remember to add index.php to the Apache DirectoryIndex.

HTML redirect (top)

In the HTML header:
<meta http-equiv="refresh" content="0;URL=http://www.redantigua.com">
Javascript redirect (top)

In the HTML header:
<script language="Javascript" type="text/javascript">
<!-- Hide script
//<![CDATA[
window.location.href="http://www.redantigua.com/"	    
//]]> End script hiding -->
</script>

Last modified: Sun May 1 23:03:34 EDT 2005