# Apache and CGI Files

**URL:** https://boards.straightdope.com/t/apache-and-cgi-files/237037
**Category:** Factual Questions
**Created:** [March 29, 2004, 6:11am UTC](https://boards.straightdope.com/t/apache-and-cgi-files/237037 "2004-03-29T06:11:55Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![caphis](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@caphis](https://boards.straightdope.com/u/caphis)
#### Post date: [March 29, 2004, 6:11am UTC](https://boards.straightdope.com/t/apache-and-cgi-files/237037/1 "2004-03-29T06:11:55Z")

</div>

I have two questions for Apache gurus here 🙂

1. My cgi-bin directory allows a directory listing in **/cgi-bin/**. Normally, this should produce a 403 error, but any file inside should execute. I can’t figure out how to produce a 403 when a directory listing is attempted, while still allowing CGI handling inside. I can either make the directory AND all files inside produce 403s, or allow a listing and execution. How can I fix this?

2. Currently, .pl and .cgi files can be executed from any directory on my server – not just inside cgi-bin. Right now, .pl and .cgi files are BOTH being run through the PERL interpreter I installed. IIRC, .pl files should be run through the PERL interpreter, and .cgi files should just be run as-is (ie: a compiled C++ program renamed .cgi could be executed). How can I fix that?

If anyone needs to see my httpd.conf file, I can provide it.

Thanks guys!

---

<div class="post-metadata">

### Author: ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)
#### Post date: [March 29, 2004, 6:13am UTC](https://boards.straightdope.com/t/apache-and-cgi-files/237037/2 "2004-03-29T06:13:56Z")

</div>

You running this on Windows by any chance? Unless you’ve got .cgi directly mapped to Perl in your conf, they should be executed by whatever is in the #! line.

---

<div class="post-metadata">

### Author: ![Q.E.D](https://avatars.discourse-cdn.com/v4/letter/q/51bf81/32.png) [@Q.E.D](https://boards.straightdope.com/u/Q.E.D)
#### Post date: [March 29, 2004, 6:16am UTC](https://boards.straightdope.com/t/apache-and-cgi-files/237037/3 "2004-03-29T06:16:40Z")

</div>

Put an blank index.html (or an index.html that says “You’re not allowed here, go away” or whatever message you want) in EVERY accessible folder that doesn’t already have an index file. This will keep the server from serving up a directory listing. I’m not sure I understand your second question, sorry.

---

<div class="post-metadata">

### Author: ![caphis](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@caphis](https://boards.straightdope.com/u/caphis)
#### Post date: [March 29, 2004, 6:21am UTC](https://boards.straightdope.com/t/apache-and-cgi-files/237037/4 "2004-03-29T06:21:39Z")

</div>

> [@friedo](#):
>
> You running this on Windows by any chance? Unless you’ve got .cgi directly mapped to Perl in your conf, they should be executed by whatever is in the #! line.

.cgi was, indeed, mapped to Perl in the conf. I removed this and tried to execute a compiled C++ file renamed to .cgi, and my server just offered me the option to save it.

---

<div class="post-metadata">

### Author: ![caphis](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@caphis](https://boards.straightdope.com/u/caphis)
#### Post date: [March 29, 2004, 6:26am UTC](https://boards.straightdope.com/t/apache-and-cgi-files/237037/5 "2004-03-29T06:26:18Z")

</div>

> [@Q.E.D.](#):
>
> Put an blank index.html (or an index.html that says “You’re not allowed here, go away” or whatever message you want) in EVERY accessible folder that doesn’t already have an index file. This will keep the server from serving up a directory listing. I’m not sure I understand your second question, sorry.

I was under the impression that there was a way to do this without using an index file, though.

---

<div class="post-metadata">

### Author: ![caphis](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@caphis](https://boards.straightdope.com/u/caphis)
#### Post date: [March 29, 2004, 6:27am UTC](https://boards.straightdope.com/t/apache-and-cgi-files/237037/6 "2004-03-29T06:27:28Z")

</div>

> [@caphis](#):
>
> .cgi was, indeed, mapped to Perl in the conf. I removed this and tried to execute a compiled C++ file renamed to .cgi, and my server just offered me the option to save it.

Also, the following file named **test.cgi** just displayed itself in the browser, without executing.

```auto

#!c:\perl\bin
# test.cgi v1.0

printf "Content-type: text/html

<html><body>";
printf "Alright.";
printf "</body></html>
";

```

---

<div class="post-metadata">

### Author: ![Q.E.D](https://avatars.discourse-cdn.com/v4/letter/q/51bf81/32.png) [@Q.E.D](https://boards.straightdope.com/u/Q.E.D)
#### Post date: [March 29, 2004, 6:30am UTC](https://boards.straightdope.com/t/apache-and-cgi-files/237037/7 "2004-03-29T06:30:12Z")

</div>

> [@caphis](#):
>
> I was under the impression that there was a way to do this without using an index file, though.

There may be, either through .htaccess or CHMOD permissions (if it’s a \*NIX server), but franky, I prefer having an index. It’s just easier, IMO. Plus you can get creative with it, rather than making folks look at a dull 403 page.

---

<div class="post-metadata">

### Author: ![caphis](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@caphis](https://boards.straightdope.com/u/caphis)
#### Post date: [March 29, 2004, 6:30am UTC](https://boards.straightdope.com/t/apache-and-cgi-files/237037/8 "2004-03-29T06:30:39Z")

</div>

Ugh… just noticed another problem. Sorry to inundate with the plethora of questions, but here goes.

Trying to access something like [http://www.myserver.com/somedirectory/index.html](http://www.myserver.com/somedirectory/index.html) works when I enter **[http://www.myserver.com/somedirectory/](http://www.myserver.com/somedirectory/)**, but not **[http://www.myserver.com/somedirectory](http://www.myserver.com/somedirectory)**.

How can I configure Apache to automatically append the trailing slash or read it as a directory if there’s no file existing with that name?

---

<div class="post-metadata">

### Author: ![caphis](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@caphis](https://boards.straightdope.com/u/caphis)
#### Post date: [March 29, 2004, 6:31am UTC](https://boards.straightdope.com/t/apache-and-cgi-files/237037/9 "2004-03-29T06:31:52Z")

</div>

> [@Q.E.D.](#):
>
> There may be, either through .htaccess or CHMOD permissions (if it’s a \*NIX server), but franky, I prefer having an index. It’s just easier, IMO. Plus you can get creative with it, rather than making folks look at a dull 403 page.

True. Thanks for the tip.

---

<div class="post-metadata">

### Author: ![caphis](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@caphis](https://boards.straightdope.com/u/caphis)
#### Post date: [March 29, 2004, 6:55am UTC](https://boards.straightdope.com/t/apache-and-cgi-files/237037/10 "2004-03-29T06:55:03Z")

</div>

Hey guys-

Managed to fix my CGI problems. The only thing I can’t figure out is appending the trailing slash to allow /somedirectory to be interpreted as /somedirectory/ and, thusly, as /somedirectory/index.html

Any ideas?

---

<div class="post-metadata">

### Author: ![Eleusis](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@Eleusis](https://boards.straightdope.com/u/Eleusis)
#### Post date: [March 29, 2004, 7:07am UTC](https://boards.straightdope.com/t/apache-and-cgi-files/237037/11 "2004-03-29T07:07:48Z")

</div>

> [@caphis](#):
>
> I was under the impression that there was a way to do this without using an index file, though.

In your httpd.conf:

```auto

ScriptAlias /cgi-bin/ "/usr/local/www/cgi-bin/"

<Directory "/usr/local/www/cgi-bin">
    AllowOverride None
    **Options None**
    Order allow,deny
    Allow from all
</Directory>

```

Alternatively:  
Options -Indexes

> [@](#):
>
> The only thing I can’t figure out is appending the trailing slash to allow /somedirectory to be interpreted as /somedirectory/ and, thusly, as /somedirectory/index.html

Do you have mod\_dir installed? It “fixes” the missing trailing slash.

---

<div class="post-metadata">

### Author: ![Eleusis](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@Eleusis](https://boards.straightdope.com/u/Eleusis)
#### Post date: [March 29, 2004, 7:11am UTC](https://boards.straightdope.com/t/apache-and-cgi-files/237037/12 "2004-03-29T07:11:42Z")

</div>

Well, of course you have it installed, or you wouldn’t be having the directory listing issues. [mod\_dir](http://httpd.apache.org/docs/mod/mod_dir.html)

I think this may be an issue with the indexing - try turning it off for the whole site in httpd.conf - and see if that solves the trailing slash problem.

---

<div class="post-metadata">

### Author: ![caphis](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@caphis](https://boards.straightdope.com/u/caphis)
#### Post date: [March 29, 2004, 7:20am UTC](https://boards.straightdope.com/t/apache-and-cgi-files/237037/13 "2004-03-29T07:20:27Z")

</div>

Thanks guys… fixed the CGI-BIN problem (a missing ScriptAlias allowed the directory contents to show). The .cgi problem was because .cgi was also associated with the PERL interpreter in Windows, itself. Fixed that.

Also, the trailing slash problem was because I had accidently mistyped part of the ServerName. That fixed it all up.

Thanks again, guys!
