Forum Moderators: coopster

Message Too Old, No Replies

Calling CGI with SSI

         

davidf

6:31 pm on Sep 13, 2002 (gmt 0)

10+ Year Member


The problem involves using SSI to call a CGI program which is designed to redirect for some User Agents. The CGI when executed by itself redirects ok, but if I call it from a page using SSI, instead of redirecting, it prints the link to the target of the redirection on the originally requested page.

I'm using print "Location:http://mysite.com/redirectionpage.html\n\n"
as my redirection command in the CGI program and call it with
<!--#exec cgi="program.cgi"-->

If the User Agent is not to be redirected then the requested page is served normally. It's only when the User Agent is to be redirected, that the originally requested page is shown with the link for the redirection target page added.

Anything I can do to get it to redirect?

heini

10:52 pm on Sep 15, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello David - welcome to WebmasterWorld!

Unfortunately I have no solution for your problem, but perhaps some of our more tech savvy members can help you?

Key_Master

11:21 pm on Sep 15, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Once the server headers have been sent you can't redirect the browser using Location: header. In other words, you can't have two sets of headers on the same page. Try a Javascript solution instead.

jatar_k

4:47 pm on Sep 16, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld davidf

Key_Master has the answer. If you are going to send headers then it has to be at the very top of the page above your opening html tag. I would try moving your ssi exec up to the top. If that doesn't work javascript may be the way to go.

davidf

9:20 pm on Sep 16, 2002 (gmt 0)

10+ Year Member


Thanks for the replies Key_Master and jatar_k. Unfortunately, I have been using the include as the first line to no avail.
Although I'm not keen on Javascript, would either of you care to elaborate on what you mean?

jatar_k

9:41 pm on Sep 16, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Ok, from apache.org [httpd.apache.org]

For example:
<!--#exec cgi="/cgi-bin/example.cgi" -->

If the script returns a Location: header instead of output, then this will be translated into an HTML anchor.

The include virtual [httpd.apache.org] element should be used in preference to exec cgi. In particular, if you need to pass additional arguments to a CGI program, using the query string, this cannot be done with exec cgi, but can be done with include virtual, as shown here:
<!--#include virtual="/cgi-bin/example.cgi?argument=value" -->

I guess that might answer the question, try include virtual and see if that works.

Key_Master

9:55 pm on Sep 16, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



davidf,

Are you on a Microsoft server? If so the syntax will be somewhat different. The following should work with Perl on Apache.

print "Pragma: no-cache\n";
print "Location: http*://www.somesite.com\n\n";

For Microsoft servers with Perl this **might** work. Never tried it before.

print "Status: 301 Moved Permanently\n","Location: http*://www.somesite.com\n\n";

Then, to make the redirect, use the include directive jatar_k referred to.

netcommr

6:37 am on Sep 17, 2002 (gmt 0)

10+ Year Member




Could you use a mod_rewrite and redirect to your cgi script. Just forget the ssi and call the script directly... then you just need to copy your page to your default redirect page.

davidf

12:04 am on Sep 18, 2002 (gmt 0)

10+ Year Member


jatar_k,

Tried the virtual include but the results were the same.

Key_Master,

I'm on a UNIX server. Added the "Pragma..." line, but no change in
results.

netcommr,

I have no knowledge of mod_rewrite, but I'll look into it.

Thanks for the advice all, maybe I need to attack this from a different angle such as mod_rewrite or?

jatar_k

12:08 am on Sep 18, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



there are many threads on mod_rewrite in this forum

Website Technology Issues [webmasterworld.com]

Do you want to maybe let us in on why you want to redirect some agents?

davidf

9:31 pm on Sep 18, 2002 (gmt 0)

10+ Year Member


jatar_k

Thanks for the link.

I created a new site about a month ago and it's attracted too many spambots and other unfriendly robots in my view. So I was looking for a way to keep them out. I'm already using .htaccess for some IPs and robots.txt for those that will obey it. The script was for the rest that showed a User Agent.

This is all probably more effort than it's worth, but it began to annoy me so I decided to do something about it.

volatilegx

11:39 pm on Sep 18, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is with your cgi script... instead of printing a new location header to redirect, try actually grabbing the page using LWP::Simple like this:

$url="http://www.domain.com/foo.html";
use LWP::Simple;
$code=get $url;
print "Content-type: text/html\n\n";
print $code;
exit;

davidf

12:38 am on Sep 19, 2002 (gmt 0)

10+ Year Member


volatilegx,

Ok, this has gotten comical. I replaced the Location line with your lines and it did display the target page...but the source page which called the script was also still served up. In other words it showed the two pages as one. I'm learning why it's called Server Side Includes.

jatar_k

12:49 am on Sep 19, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



looks like cloaking to me. ;)

then for the non redirected ua's just serve the other content.

if ($redirectua) {
$url="http://www.domain.com/foo.html";
} else {
$url="http://www.domain.com/bar.html";
}
use LWP::Simple;
$code=get $url;
print "Content-type: text/html\n\n";
print $code;
exit;

jatar_k

12:53 am on Sep 19, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you might also want to read this monster
A Close to perfect .htaccess ban list [webmasterworld.com]

davidf

4:06 pm on Sep 20, 2002 (gmt 0)

10+ Year Member


To anyone curious about this, I finally solved the redirection problem with a meta refresh.

if ($badbot == 1){
$url="http://domain.com/badbotpage.html";
print "Content-type: text/html\n\n";
print "<HTML>";
$redir = "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0;URL=";
$redir .= $url."\">";
print $redir;
print "</HTML>";
exit;
}

Key_Master

5:27 pm on Sep 20, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I doubt that a meta refresh is going to redirect a spam bot. You need to figure out a way to ban the bot once it hits your site.