Forum Moderators: coopster & phranque

Message Too Old, No Replies

Using a Directory instead of .cgi or .pl

How can you do this?

         

treeline

4:26 am on Mar 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd like to use a directory url instead of having to give the url of a specific .cgi or .pl file. For example:

site.com/widget

to run a perl script instead of:

site.com/widget/widget.cgi

I'm on a linux/apache server with access to server side includes and .htaccess, but don't really understand .htaccess .

Is this possible to do? I'd appreciate any help.

SeanW

2:28 pm on Mar 29, 2004 (gmt 0)

10+ Year Member



You can do it with

ScriptAlias /widget/ "/var/www/widget/"
DirectoryIndex widget.cgi

Everyting in widget will be executed as a CGI script, btw. Otherwise use a file/location container and a force-type. This is probably better asked on the Apache forum

Sean

Nova Reticulis

2:49 pm on Mar 29, 2004 (gmt 0)

10+ Year Member



Use mod_rewrite [httpd.apache.org] for anything more complicated than the example above, for instance when you want to pass parameters to the script. Search google for "search engine friendly URLs", there's a lot of material describing how people do it.

treeline

10:35 pm on Mar 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for your suggestions on this, I appreciate it!

kenta

7:59 pm on Apr 1, 2004 (gmt 0)

10+ Year Member



I just set index.cgi as a valid "DirectoryIndex" type in the apache.conf(or httpd.conf). At this point renaming your current .cgi to "index.cgi" will cause it to execute when opening the directory (assuming that index.html or others are not present).

Hissingsid

1:01 pm on Apr 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

This is what I do:

Put this in your root .htaccess file.

<FilesMatch "^widget$">

ForceType application/x-httpd-cgi

</FilesMatch>

Then you need something like this in your cgi script which you call widget.cgi. This reads URLs in the form [test.com...]

#!/usr/bin/perl

use CGI;

$q = new CGI;

$path = $q ->path_info();

#YOU MUST INSERT a regex test here to stop rogues sending dangerous characters into your script.

@path = split(/\//, $path);

#If you know @path always has three values in it you know that $path [2] is in this case "string" and use that to match with a key in your database

Best wishes

Sid

tombola

8:09 pm on Apr 2, 2004 (gmt 0)

10+ Year Member



It's much easier than that:
if you want site.com/widget instead of site.com/widget/widget.cgi use Content Negotiation (Apache).

Put this line in your .htaccess file:

Options ExecCGI MultiViews

Put widget.cgi in the appropriate directory:
site.com/widget.cgi.
With content negotiation, you can omit the filename extension, so your URL could be:
site.com/widget

Hissingsid

9:04 am on Apr 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Tombola,

I didn't realise I could do that in my .htaccess file. I've done a change on my httpd.conf file on my local machine but forgot all about it. A middle aged moment ;)

Great tip! Thanks

Sid