[Tutor] URL redirect cgi script
Sheila King
sheila@thinkspot.net
Thu, 17 May 2001 13:22:50 -0700
OK, this is something I thought I knew how to do. I have no idea why my script
isn't work correctly.
I have an IP number that I want to prohibit from my website. Basically, I want
to redirect any HTTP requests from this IP number to some other web address
(possibly within my own site, or maybe not. That's not the important thing.)
This script, called 403.py, is supposed to be my 403 error document on an Apache
web server.
Here is the document:
---------------------------------------------------------
#!/big/dom/xthinkspot/bin/python2.0
import os, httplib
denyList =['127.0.0.1', '128.158.104.168']
if str(os.environ["REMOTE_ADDR"]) in denyList:
print r"Location: http://www.futurequest.net"
print
print
else:
print r"Location: http://www.k12groups.org/403.html"
print
print
---------------------------------------------------------
Now, the 127.0.0.1 I had replaced with my own IP address, and I have a .htaccess
document on the website, and I also put my IP address in there, to deny. (I have
to test this somehow, right?)
When I went to the website, I can tell that the script is being called. I
checked it in Sam Spade, and here is the output of the HTTP request:
---------------------------------------------------------
05/17/01 13:17:44 Browsing http://www.k12groups.org
Fetching http://www.k12groups.org/ ...
GET / HTTP/1.1
Host: www.k12groups.org
Connection: close
User-Agent: Sam Spade 1.14
HTTP/1.1 403 Forbidden
Date: Thu, 17 May 2001 20:18:07 GMT
Server: Apache/1.3.17 (Unix) mod_macro/1.1.1 PHP/4.0.4pl1
Location: http://www.futurequest.net
Connection: close
Transfer-Encoding: chunked
Content-Type: text/plain
1
0
---------------------------------------------------------
So, the script is being called. However, it is not redirecting to the URL in the
Location: header. It basically stays at my site, with a completely blank page
(since no HTML code is being sent). Matter of fact, I'm surprised that the
Content-Type: text/plain is being sent. I've tried with fewer print statements.
(I thought I should need only two print statements: (1) to send the Location
header, and (2) to send a blank line to indicate the end of the headers.) But
when I take out the second blank print statement, I get a "document contained no
data" error message.
I've gone round and round on this, and I'm tearing my hair out over it.
If anyone has any ideas, I'd sure appreciate a tip.
--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/