RE: URL redirection (longish)

New Message Reply Date view Thread view Subject view Author view Attachment view

From: Nick Ciesinski (fletch26@charter.net)
Date: Wed Oct 18 2000 - 20:09:28 CDT


I like your idea with the redirect for the index.html file. I was looking
at doing this on our server. The only problem I have with this is getting
the perl script to work for our setup. We have over 20 subnets for our
students and each subnet has 20 or so IP's (x.x.x.230-254) for
non-registered clients. My main problem is because I am not a perl wizard I
can't figure out a small and efficient perl script to do the redirect for
all the different non-registered IP's. I surely don't want a perl script
with a "if then" statment for each possible non registered IP, that would
take forever to run. Does anyone have any ideas on how I could do this? I
am sure someone out there is a perl wizard ;)

Thanks,

Nick Ciesinski
University Wisconsin Whitewater

-----Original Message-----
From: owner-netreg@southwestern.edu
[mailto:owner-netreg@southwestern.edu]On Behalf Of Steve Hideg
Sent: Wednesday, October 18, 2000 2:57 PM
To: netreg@southwestern.edu
Subject: URL redirection (longish)

One behavior I noticed on web browsers and the netreg apache setup is
if an unregistered machine attempts to go to some URL that isn't the
netreg web server, they get sent to the registration page, but the
URL in the browser still points elsewhere (e.g. http://www.yahoo.com
displays the registration page).

I'm not sure why this is. Apparently Apache isn't sending a redirect
command to the browser, it is simply returning the page specified in
the ErrorDocument 404 entry in httpd.conf (which is normally set to
"/" in netreg 1.x).

I think a better approach would be to specify a page that redirects
the browser to the correct URL.

You can construct a page with a META tag, something like this:

<HTML>
<HEAD>
<TITLE></TITLE>
<META HTTP-EQUIV="REFRESH" CONTENT="0;
URL=http://resnet-reg.saintmarys.edu/">
</HEAD>
<BODY>
</BODY>
</HTML>

Which actually redirects the browser to / (the document root). Call
it something like "redirect.html" and point the ErrorDocument
declarations in httpd.conf to it:

ErrorDocument 404 /redirect.html
ErrorDocument 403 /redirect.html

I've taken this a step further. I want to be able to use this web
server for other purposes, and sending non-resnet-registration users
to the registration page on errors would be confusing. So, I did the
following:

* index.html at document root redirects to a perl script. index.html
contains the following meta tag:

   <META HTTP-EQUIV="REFRESH" CONTENT="0; URL=/cgi-bin/redirect.pl">

* redirect.pl is a script that sends redirect commands to the web
browser depending on the client's IP address. It uses the CGI.pm
module:

#!/usr/bin/perl

#---------------------------------------------------------------------------
-\
# redirect.pl
# Perl script that issues redirect commands
# based on the IP address of the client
# October 2000
# Steve Hideg
#---------------------------------------------------------------------------
-/

use CGI ':standard';

if (remote_addr()=~/^147\.53\.199\./)
        {
        print redirect("http://resnet-reg.saintmarys.edu/register/");
        }
else
        {
        print redirect("http://resnet-reg.saintmarys.edu/index2.html");
        }

Note that subnet 199 is our unregistered pool. Obviously, you can
enhance this and make it as smart as you want/need.

* Create a file index2.html, which is the "real" index page at
document root. You can use it for non-netreg purposes (or netreg
admin stuff, etc.)

* In httpd.conf, I put these entries:

ErrorDocument 404 /cgi-bin/redirect404.pl
ErrorDocument 403 /cgi-bin/redirect403.pl

* Two more perl scripts handle redirection on 404 and 403 errors:

redirect404.pl:

#!/usr/bin/perl

#---------------------------------------------------------------------------
-\
# redirect404.pl
# Perl script that issues redirect commands for error 404 responses
# based on the IP address of the client
# October 2000
# Steve Hideg
#---------------------------------------------------------------------------
-/

use CGI ':standard';

if (remote_addr()=~/^147\.53\.199\./)
        {
        print redirect("http://resnet-reg.saintmarys.edu/register/");
        }
else
        {
        $theURL = url();
        $sw = server_software();
        $sp = server_port();
        print header(-status=>404,
                     -type=>'text/html');
        print start_html(-title=>'404 Not Found');
        print "<H1>Not Found</H1>\n";
        print "<P>The requested URL \"$theURL\" was not found on this
server.</P>\n";
        print "<HR>\n";
        print "<I>$sw at resnet-reg.saintmarys.edu Port $sp</I>\n";
        print end_html();
        }

redirect403.html:

#!/usr/bin/perl

#---------------------------------------------------------------------------
-\
# redirect403.pl
# Perl script that issues redirect commands for error 403 responses
# based on the IP address of the client
# October 2000
# Steve Hideg
#---------------------------------------------------------------------------
-/

use CGI ':standard';

if (remote_addr()=~/^147\.53\.199\./)
        {
        print redirect("http://resnet-reg.saintmarys.edu/register/");
        }
else
        {
        $theURL = url();
        $sw = server_software();
        $sp = server_port();
        print header(-status=>403,
                   -type=>'text/html');
        print start_html(-title=>'403 Forbidden');
        print "<H1>Forbidden</H1>\n";
        print "<P>You are not allowed to access URL \"$theURL\" on
this server.</P>\n";
        print "<HR>\n";
        print "<I>$sw at resnet-reg.saintmarys.edu Port $sp</I>\n";
        print end_html();
        }

Anyway, I think this makes steering browsers to the correct page a
little smarter. Go ahead and use these scripts if they are useful to
you. If anybody has a nicer way of doing this, I'd like to hear about
it. My experience with Apache has been minimal.

++Steve

**********************************************************************
To unsubscribe from this list, send an e-mail message to
majordomo@southwestern.edu containing a single line with the words:
unsubscribe netreg
Send requests for assistance to: owner-netreg@southwestern.edu
**********************************************************************

**********************************************************************
To unsubscribe from this list, send an e-mail message to
majordomo@southwestern.edu containing a single line with the words:
unsubscribe netreg
Send requests for assistance to: owner-netreg@southwestern.edu
**********************************************************************


New Message Reply Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.4 : Thu Aug 12 2004 - 12:01:34 CDT