do you also like to place your CGI scripts in any directories, not
only in the cgi-bin directory? Once I tried it and added AddHandler
cgi-script .cgi to my .htaccess, but it didn't work. Maybe I was
wrong, or infinity's configuration does not allow it for some reason -
doesn't matter, I've found another way: a rewrite rule causes URLs
with trailing .cgi to be directed to a single script in cgi-bin/ ,
which in turn automatically executes the CGI scripts in different
directories. See below the details.
For the rewrite rule in public_html/.htaccess:
- Code: Select all
RewriteEngine On
RewriteBase /
RewriteRule \.cgi$ cgi-bin/cgi [L]
- Code: Select all
#!/usr/bin/env gosh
;;-*- scheme -*-
;; Copyright (c) 2006 Thomas Hafner <[email protected]>
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; History:
;;
;; v0.1 2006-05-24 Thomas Hafner: First version.
(define (apply-r al f)
(apply f al))
(define (die error)
(format #t "Content-Type: text/plain\n\nERROR: ~a\n" error)
(exit))
(define (getenv-or-die varname)
(or (sys-getenv varname)
(die (format #f "Environment variable ~s not set." varname))))
(define (split-path path)
(rxmatch #/^(.*?\/)(.*)$/ path))
(define (remove-common-dirs x y k)
(let ((mx (split-path x))
(my (split-path y)))
(if (and mx my (string=? (mx 1) (my 1)))
(remove-common-dirs (mx 2) (my 2) k)
(k x y))))
(define (mk-replace-trailing-string from to)
(let ((fromsl (string-length from)))
(lambda (s)
(let* ((ssl (string-length s))
(s1sl (- ssl fromsl)))
(and (>= s1sl 0)
(let ((s1 (substring s 0 s1sl))
(s2 (substring s s1sl ssl)))
(and (string=? from s2)
(string-append s1 to))))))))
(apply-r
(map getenv-or-die '("SCRIPT_NAME" "REDIRECT_URL" "SCRIPT_FILENAME"))
(lambda (script_name redirect_url script_filename)
(remove-common-dirs
script_name redirect_url
(lambda (from to)
(define proc (mk-replace-trailing-string from to))
(let ((nsf (proc script_filename))
(nsn (proc script_name)))
(sys-putenv "SCRIPT_FILENAME" nsf)
(sys-putenv "SCRIPT_NAME" nsn)
(and (sys-access nsf (logior R_OK X_OK))
(sys-chdir (sys-dirname nsf))
(sys-exec nsf (list nsf)))
(die (format #f "~s could not be executed." nsf)))))))
Gauche Scheme. Of course this simple task could be done in whatever
programming language (and probably somebody did it already), but it
was real fun to use Gauche.
I didn't try, but I guess the script may fail if the path and/or URL
contain non ASCII characters.
Regards
Thomas



