Forum Moderators: coopster & phranque

Message Too Old, No Replies

cgi script to html file

         

emma matthews

1:02 pm on Dec 12, 2002 (gmt 0)

10+ Year Member



I would like to set up my server so if someone goes to, for example,

[mydomain.com...] the script [mydomain.com...] is run and the output displayed, invisible to the user.

Similarly if they go to [mydomain.com...] the script [mydomain.com...] is run.

This should work for whatever html page they go to(i.e not pre determined).

Can this be done by changing the .htaccess file?

ScottM

2:25 pm on Dec 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was just reading something on this. I hope this is what you are looking for:

Rewrite URL [httpd.apache.org]

I think what you are looking for is at or near the bottom?

emma matthews

3:29 pm on Dec 12, 2002 (gmt 0)

10+ Year Member



Thanks ScottM,

I assume you are talking about the sections entitled:

* From Static to Dynamic
* On-the-fly Content-Regeneration

However, would someone mind explaining this to me in simpler terms(I am all but a beginner at this:-) How does this relate to what I want to do, as in translating script.cgi?keyword=apples into apples.html. Also how does this relate to the .htaccess file?

Any help, much appreciated.

jdMorgan

3:40 pm on Dec 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Emma,

Every time a page or object (a file) is requested from your server, the .htaccess files in the path along the way to that file are examined by the server. mod_rewrite directives can be included in any or all of those .htaccess files. mod_rewrite is used to rewrite URLs or pathnames - to modifiy them.

Therefore, you can write a set of directives for mod_rewrite to use to translate a request for
/<anyfruit>.html into a request for /script.cgi?keyword=<anyfruit>.

This can be used to translate "friendly" URLs requested by visitors and search engine robots into the script path and parameters you need. The flip side is that you need your script to output friendly URLs as well so that these URLs will be used for bookmarks and search engine listings. It is up to the script to do, since .htaccess is processed only before a request is served.

HTH,
Jim

emma matthews

4:26 pm on Dec 12, 2002 (gmt 0)

10+ Year Member



Thanks Jim. I understand this much better now. My script already outputs user friendly urls. However, I am still a little unsure what to do to my .htaccess file to achieve my objectives.

The current file has only one line
--------------------
ErrorDocument 404 /404.html
--------------------

What exactly do I need to add to this to tanslate to translate a request for /<anyfruit>.html into a request for /script.cgi?keyword=<anyfruit>?

seindal

4:32 pm on Dec 12, 2002 (gmt 0)

10+ Year Member



Can you change the cgi program?

If yes, have it read the PATH_INFO data instead of the cgi-input variables. Just strip off the .html extension and any leading /, and use the rest as your keyword.

Then have you script run for all files in the folder. With apache this would be

ScriptAlias / <server-dir>/cgi-bin/script.cgi

This means that /apples.html will be changed into

/cgi-bin/script.cgi/apples.html

The superflous part of the path is the PATH_INFO, in this case /apples.html

This might be easier to handle than mod_rewrite, which can be rather daunting.

René.

ScottM

4:47 pm on Dec 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Emma, I'm a beginner on this also. I'm learning it as I type this.

Here is a good thread:

[webmasterworld.com...]

I'm still looking for a good beginners tutorial.

From post #8 of the above link:

The tricks of the trade involve using mod_rewrite to serve requests for /news/12.html from news.php?id=12

Assuming you have apache with mod_rewrite enabled then that example could be accomplished with:

RewriteEngine on
RewriteRule ^/news/(.*)\.html$ /news.php?id=$1

I would sure like the 'rule' portion explained, step by step.

I realize it's not cgi, but I imagine the rule is similar.

jdMorgan

5:09 pm on Dec 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What exactly do I need to add to this to tanslate to translate a request for /<anyfruit>.html into a request for /script.cgi?keyword=<anyfruit>?


ErrorDocument 404 /404.html
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ /script.cgi?keyword=$1 [L]

Basically, the "$1" back-references the contents of the parentheses, which are specified here as "anything preceding '.html'."

Mod_rewrite is very powerful. It is also complex in and of itself, and complex because it uses regular expressions pattern-matching. For more info, see the Apache mod_rewrite documentation [httpd.apache.org], and this short regex primer [etext.lib.virginia.edu].

HTH,
Jim

emma matthews

5:11 pm on Dec 12, 2002 (gmt 0)

10+ Year Member



seindal:
In this case I can, however I don't know where to start. How can I get the script(written in perl) to get the PATH_INFO data instead of the cgi-input variables, and strip off the .html extension and any leading /, and use the rest as the keyword.

At the momemnt, the script is:
use CGI qw/:standard/;
use HTTP::Request;
use LWP::UserAgent;

$query = new CGI;
$ua = LWP::UserAgent->new;

$st = $query->param('keyword');

so for script.cgi?keyword=apples, in this script $st takes the value of apples. So, instead how can I get $st to take
apples from /apples.html(-->/cgi-bin/script.cgi/apples.html )

seindal

5:47 pm on Dec 12, 2002 (gmt 0)

10+ Year Member



The following is not tested, but you can try it out


$st = $query->param('keyword');
unless ($st) {
$st = $ENV{PATH_INFO};
if ($st) {
$st =~ s/\.html?$//;
$st =~ s/^\///;
}
}

emma matthews

2:09 pm on Jan 7, 2003 (gmt 0)

10+ Year Member



jdMorgan - thanks for this, it's working great. However, how can I stop certain given pages from being treated like this?

For example if the user goes to 'index.htm' I do not want the cgi script to be executed(and instead want index.htm to be displayed).