Solved

Redirection

Posted on 2000-04-14
17
356 Views
Last Modified: 2013-12-25
I've researched the archive and the common concensus for a CGI to perform redirection is:

printf("Location: <url>\r\n\r\n");

However, I have not been able to get this to work with either Netscape 4.6 or Internet Explorer 4.0.  Netscape returns a dialogbox with the message "The document contained no data".  IE returns a dialogbox with the message "The server returned an invalid or unrecognized response".

I'm trying to develop a general purpose CGI in C++ that uses an argument from the input string to determine the destination CGI. The overriding reason is to "disable" the browser Back button.  However, I'm first trying to get something to work with a fixed URL.  The code (redirect.exe) for that is as follows:

void
main(int argc, char** argv)
{
  cout << "Location: http://www.halcyon.com/msbinder/links/\r\n\r\n";
}

The URL is my Home Page so I know it exists.

I'm calling the CGI from a start up page with the following URL:

<a href="http://boss7_man/cgi-bin/redirect.exe/add?REDIRECT=add_main%2Eexe"><font size="4"><strong>Start Up (Redirect CGI)</strong></font></a>

'boss7_man' is a Personal Web Server from MS Visual Studio; my development environment.

I've already tested a static page redirection (<meta http-equiv ...>) and that works with the browsers I'm using.
0
Comment
Question by:msbinder
17 Comments
 
LVL 2

Expert Comment

by:mattrope
ID: 2718615
I think in order for the Location directive to work, you'll still have to generate a Content-type header, even though you don't intend to display anything.  For example:

cout << "Location: www.here.com\n";
cout << "Content-type: text/html\n\n";
return(0);

You're getting the "No Data" error because the web browser doesn't see you start a page, so it's probably ignoring the Location directive.  Hope that helps.
0
 

Author Comment

by:msbinder
ID: 2724717
I tried several permutations and none of them worked. i.e. as follows:

1)
Location: <url>\r\n
Content-type: text\html\r\n\r\n

Result: same error as before.

2)
Content-type: text\html\r\n\r\n
Location: <url>\r\n

Result: Screen shows "Location:<url>" as expected.

3)
Content-type: text\html\r\n
Location: <url>\r\n

Result: Screen shows "The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:
Content-type: text/html
Location: http://www.halcyon.com/msbinder/links/"  as expected (need 2 [cr][lf] after the last directive.

4)
Content-type: text\html\r\n
Location: <url>\r\n\r\n

Result: Same error as before.

------

I put an HTTP-EQUIV=Refresh etc. into the header of an otherwise empty page (without the "Location: ..." directive) and it worked, but that's not the same thing.  NOTE: this redirect effectively disables the 'Back' button using Netscape, but not in IE 4.0, but that's a different issue.

Is there some sort of browser directive needed to make this work?  I rather doubt that due to the responses on this subject that I see here,  but it is a possibility.  I'm working on getting a version of this on my home computer to test there.
0
 

Expert Comment

by:rickhall2000
ID: 2731366
You can use a JavaScript to change the location.  I used Perl for when I had to do it, but it is easily adaptable to C++.  In this, $filename had the URL I wanted.  

    print "<SCRIPT LANGUAGE=\"JavaScript\">";
    print "location=\"$filename\";";
    print "</SCRIPT>";

In the end, what the page needs to have in it is:

<SCRIPT LANGUAGE="JavaScript">
location="www.whatever.com";
</SCRIPT>
0
 

Author Comment

by:msbinder
ID: 2732278
I'll keep that in mind Rickhall.  To be honest I don't know much about JavaScript, but it's one of those things I have slated to take training on.   Also, JavaScript can be "turned off" on a browser which would probably take away the functionality I'm after.
0
 

Expert Comment

by:krirob
ID: 2919442
first send
"Content-type: text/html\n"
to the browser only 1 "\n" then
"Location: www.here.com\n"

and it will work.


0
 

Author Comment

by:msbinder
ID: 2955675
That's the way it SHOULD work, krirob, but for some reason in my set up it doesn't.  I tested that permutation with the result listed above.  Placing \r\n or only \n doesn't matter for most browsers, and it doesn't matter for mine.  The number and location of \n is important, but the "right" one doesn't work in my environment.  There's something else I don't know about affecting this; probably the MS personal web server I'm using.
0
 

Expert Comment

by:Web_Tech
ID: 3074531
In perl this is what works.

print "Location: http://www.whereever.com\n\n\n\n";


You can replace the url with a variable but it must include the "http://" or you will get the errors that you are describing..

Web_Tech
0
 

Expert Comment

by:thaxi
ID: 3114954
I am assuming you are doing this in C, if so, this is how I have done it successfully on my server.


void main (void)
  {
printf( "Status: 302 Moved Temporarily%c", 10 );
printf( "URI: %s%c", url, 10 );
printf( "Location: %s%c", url, 10 );
printf( "Method: GET%c%c", 10, 10 );
  }


where url is the string that can be passed to this CGI function.


I have used it and it works great with IE 4.01 and Netscape 4.x

-Aashish Thakker <thaxi>
athakker@usc.edu
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:thaxi
ID: 3127359
You can test to see if it works by going accessing


http://thaxi.usc.edu/cgi-bin/redirect.exe and specifying a URL you wish to redirect to..

e.g to go http://www.yahoo.com simply access

http://thaxi.usc.edu/cgi-bin/redirect.exe?http://www.yahoo.com

The source is exactly the same as above, except for checking to see if a URL was specified.

Let me know of you think my code answered your question.

Thanks,
Aashish <athakker@usc.edu>

-thaxi
0
 

Author Comment

by:msbinder
ID: 3130553
The problem, thaxi, is apparently not the Location: server directive (There is consistent concensus on the format that is supposed to work), but is likely to be an issue with the server itself.  I'm using the MS personal webserver for this.  What server are you using?  If not the MS personal webserver then perhaps you can check your redirection CGI on that server and let me know what you get.

BTW, I appreciate the feed back I've received so far.  It has at least directed the search toward where my actual problem exists.
0
 

Expert Comment

by:thaxi
ID: 3144400
Hi msbinder, I am using a Netscape Enterprise Server and have CGI setup a directory-type. I am not sure I get your comment on the error you are getting. What error ARE you getting? when I run my code on my server, it is working. I am not sure why PWS would not work. If you are able to get other CGI programs working, I would think this should work, though I maybe wrong since I have never worked with Microsoft PWS. BTW the exact source for my program is as follows  :

# include <stdio.h>
# include <stdlib.h>
# include <string.h>

void main (int argc, char *argv[])
  {
       if(argc==1) /* If no URL is specified, output an error message to user */
            {
             printf( "Content-type: text/html\n\n" ); /* Outut MIME Header*/
             printf( "<HTML>\n" );
             printf( "<HEAD>\n" );
             printf( "<TITLE>CGI Process Output</TITLE>\n" );
             printf( "No URL specified, cannot redirect to a blank URL\n");
             printf( "</HEAD>\n" );
             printf( "<BODY>\n" );
             exit(1); /*As there is no URL specified, we cannot redirect, hence exit */
            }
       /* Output following code to redirect client browser to specified URL */
       printf( "Status: 302 Moved Temporarily%c", 10 );
       printf( "URI: %s%c", argv[1], 10 );
       printf( "Location: %s%c", argv[1], 10 );
       printf( "Method: GET%c%c", 10, 10 );
  }



Let me know of this is any help at all...

-thaxi
0
 

Expert Comment

by:thaxi
ID: 3235758
I am wondering if my code helped you? do let me know.

-thaxi
0
 

Author Comment

by:msbinder
ID: 3334043
Thanks thaxi, your code is very similar to mine.  It shouldn't matter which webserver is being used, but since that is where the CGI is being implemented, then it naturally follows that the problem is there since the same code works on other servers.  I haven't had a chance, recently, to investigate my MS PWS setup.  I'll be sure to post here if I DO find anything out.
0
 

Expert Comment

by:thaxi
ID: 3391153
Hi msbinder,

I was wondering if you got the chance to take a look at your MS PWS config? I installed PWS on a 98 SE machone and created a virtual directory cgi-bin with only execute permissions. I placed my executable of the above code in this directory and it worked right-away!!
Let me know what sort of a configuration you have.
0
 

Accepted Solution

by:
thaxi earned 50 total points
ID: 3574012
Did it work for you?? if it did, maybe you want to assign your points to the person who answered your question....
0
 

Expert Comment

by:thaxi
ID: 3928933
Is there an admin on here who oversees things? I dont seem to find anyone responding to this?
0
 
LVL 3

Expert Comment

by:darinw
ID: 4144722
Moving question to PAQ.

darinw
Community Support
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now