1
votes

I'm setting up an apache webserver (on Ubuntu 18.04) with a CGI script, but I want a clean URL without "cgi" in it.

I already have a functioning script (in Perl), which, for the purposes of this question, I'll call myscript.

1. localhost/cgi-bin/myscript (works)

If I put the script in /usr/lib/cgi-bin/myscript, it works with URL localhost/cgi-bin/myscript.

2. localhost/myscript.cgi (works)

Alternatively, I can reconfigure /var/www/ to run files with .cgi or .pl extensions as CGI:

 <Directory /var/www/>
     Options Indexes FollowSymLinks
     AllowOverride None
     Require all granted
+    Options +ExecCGI
+    AddHandler cgi-script .cgi .pl
 </Directory>

This works IF I add a .cgi extention: myscript.cgi. Then URL localhost/myscript.cgi works.

3. localhost/myscript ???

But I don't want cgi in my URL. I just want localhost/myscript to run myscript as CGI. (And I don't want to force other files in ROOT to be CGI).

Is this possible?

1

1 Answers

0
votes

You cannot do that directly. However, you could Redirect requests for specific extension-less URLs to their corresponding .cgi.

You can test these with an .htaccess file to avoid having to reload the server.

In your <VirtualHost ...> section :

<Directory /your/web/dir/>
  Require all granted
  AllowOverride All
</Directory>

In /your/web/dir/.htaccess :

Options +ExecCGI
AddHandler cgi-script .cgi
Redirect  "/test" /test.cgi
# or
# RedirectMatch  "^/(test)$" /$1.cgi

Another possibility would be to use mod_rewrite, and redirect calls to any existing executable file to a file with the same name but with a .cgi extension. The extension-less file must only exist, and can be empty.

In /your/web/dir/.htaccess :

Options +ExecCGI
AddHandler cgi-script .cgi

RewriteEngine On
# If not already .cgi and executable, redirect to .cgi
RewriteCond /your/web/dir/%{REQUEST_URI} "!\.cgi$"
RewriteCond /your/web/dir/%{REQUEST_URI} -x
RewriteRule ^(.+) /$1.cgi [R]

Of course, you can use any other extension than .cgi. For example .x if you set AddHandler cgi-script .x.