Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have this shopping cart script, now I need help with this, you see it creates a directories for each category you make so say you made a category called Snowmobile and now you can reach it at http://someurl.com/shop/Snowmobile and for each directory I make it has to have a something call htpp://someurl.com/shop/category.cgi to read that category

So now we made a category but now should I put a index.shtml in there and call category.cgi and have a file in there called num.txt that has its id number to identify it.

\shop\ - category.cgi (Displays that category) \shop\Snowmobile\ - index.shtml (Call category.cgi) - num.txt (Get the id number)

or...
I can use.htaccess using redirect funtion to redirect the index.html page to category.cgi with num.txt

I just need to find a way to do this but i need to create the directories. Just need an idea.

Replies are listed 'Best First'.
Re: SSI or .HTACCESS for CGI
by Kanji (Parson) on Nov 10, 2002 at 18:26 UTC

    You don't say what web server this is under, but I'm going to guess Apache from the .htaccess reference. If so, then you can create a single .htaccess file under /shop/ and have it set the default index page for all directories under and including /shop/ to use your CGI, like so...

    DirectoryIndex /shop/category.cgi

    Then you can peek in $ENV{'REQUEST_URI'} to determine what category was requested.

        --k.


      Since you're using CGI, you probably should make use of the methods that it provides for this sort of thing instead of using the environment variables. It's not going to make much of a difference under Apache, but if you ever try to port this to another Web server platform, you'll be glad you did this:
      my $path = $cgi->path_info();
      This should return any parameters received after the CGI program itself. Give it a bit of testing to see how to map this back in to your cart namespace.
Re: SSI or .HTACCESS for CGI
by Anonymous Monk on Nov 10, 2002 at 19:14 UTC
    $a = $ENV{REQUEST_URI}; $a =~ s|(/.*/)|$1|i; $a = $1; $sth = $dbh->prepare("SELECT * FROM category WHERE url=?"); $rows = $sth->execute($a);
    See in my databse I have it so each category has its url so heres and example

    Category: Accessories URL: /Snowmobile/Accesories/
    See how would I make it so they went to the url without a trailing slash like http://someurl.com/shop/Snowmobile/Accesories

    and in my code it doesnt get it right cause the /shop/ is in $a, I just need an idea how I would do this.

      You can be a little more selective in your matching, and include logic to append a trailing slash if there isn't one already.

      if ( $ENV{'REQUEST_URI'} =~ m|\A/shop(/.*)$| ) { my $url = $1; $url .= '/' if $url !~ m|/$|; $sth = $dbh->prepapre("..."); # ... }

      However, if you're working with real directories, you shouldn't encounter the trailing slash problem as Apache automatically redirects such requests to the proper location IIRC.

      BTW, is there anything else in /shop/? If not, rather than using physical directories, why not rename your script to 'shop'?

      You wouldn't need to create any subdirectories then as they'd all be virtual extensions of your CGI, with the requested directory appearing in $ENV{'PATH_INFO'} alread minus the /shop/ portion but definitely in need of some trailing slash massaging.

          --k.


Re: SSI or .HTACCESS for CGI
by Anonymous Monk on Nov 11, 2002 at 02:15 UTC
    New Problem here, I just made it so that it could read the query string. but thats not the problem, I made these direcotories for a search engine to spider, will it spider since theres no index file??