Forum Moderators: phranque

Message Too Old, No Replies

Printing a redirect

How to make it work!

         

carfac

5:52 pm on Oct 22, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi:

I have been trying to modify a mod_perl script, and I am not sure of the syntax to make this print a page off the site...

This script will look for requests for bad filenames (stuff like requests for formmail.pl and other, hacker sorts of exploit hacks) and redirect them to a null server. I do it now with mod_rewrite, and that works fine, I just thought it might be a bit better in mod_perl. My problem is I am not sure of the syntax to make the redirect (the rest of the script works fine.

Here is a snippet of gode:

return OK if $r->uri eq "/robots.txt";
return OK if $r->uri eq "/error/err403.html";
return SERVER_ERROR unless my $sub = get_match_sub($r,$patfile);
return OK if $sub->($agent);
$r->log_reason("Bad Request: $agent ($agent1)",$r->filename);
print "Location: [null.server";...]

The "print: Location line is what needs to be made to work. When run as above, it does not redirect to the null.server... rather, the present server returns a 404. I would want this to not show the redirect to the client, but just hang them up!

thanks!

dave

andreasfriedrich

8:23 pm on Oct 22, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A redirect in mod_perl is done by setting the location header field to the new URI and returning MOVED (permanent) or REDIRECT (temporarily). I´m not sure if you can do that in an access handler routine and I cannot test it. (My server is down with a HD failure. :() You need to try for yourself:

$r->header_out(Location => 'http://null.server/'); 
return REDIRECT;

However, I´m afraid that code will not do what you want:

I would want this to not show the redirect to the client, but just hang them up!

A redirect can either be external or internal to the server.

  • The external one is done as explained above. It results in a HTTP response header like this one:

    HTTP 1.1 302 Moved Temporarily 
    Location: [null.server ]

    To be HTTP compliant Apache will add a HTML document as the message body.

    It is then up to the UA to request that new URI. Most UAs will update the URI in their address bar. The redirect is shown to the client.

  • The internal redirect can be used only for redirects to URIs on your own server. With an internal redirect the server pretends that the UA made the request to the new URI. The UA will not see the redirect but you cannot redirect to [null.server...] unless that is your own ;)

Hope this helps.

Andreas

carfac

8:50 pm on Oct 22, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Andreas:

As always, you are a tremendous help. Thank you!

Sorry about the HD.... hope you get that fixed soon!

dave