Forum Moderators: phranque

Message Too Old, No Replies

How can I use extensionless cgi scripts outside the cgi-bin?

I want to be able to use extensionless cgi scripts outside of my cgi-bin

         

rsgalloway

7:05 pm on Sep 29, 2005 (gmt 0)

10+ Year Member



I want to be able to use python cgi scripts outside of my cgi-bin (eg. foo.com/somescript?x=1) but I still need to be able to load images from the foo.com/images dir and it would be nice to still be able to load .html files. The way I have it setup now, I can only do one or the other:

ServerName foo.bar.com
DocumentRoot /home/bar.com/foo
ScriptAlias / "/home/bar.com/foo/"
<Directory "/home/bar.com/foo">
AllowOverride All
Options none
Order allow,deny
Allow from all
</Directory>
ErrorDocument 403 [foo.bar.com...]
ErrorDocument 404 [foo.bar.com...]
DirectoryIndex script

The above directives allow me run my scripts from outside the cgi-bin (eg. foo.com/somescript?x=1) but index.html doesn't load anymore and Apache tries to executes image files:

Premature end of script headers: bk_button_end.gif

Any help appreciated.

jdMorgan

12:19 am on Sep 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This should execute any extensionless file in /home/bar.com/foo/ as a cgi, but leave all other paths and files with extensions alone:

ScriptAliasMatch ^/([^/.]*)$ "/home/bar.com/foo/$1"

This replaces your current ScriptAlias directive. The pattern matched is: "Any URL in the root directory (URL does not contain a slash other than the one at the beginning) and with no extension (URL does not contain a period)." I'm not sure if that's exactly what you want, but it illustrates the concept.

You could do the same thing using a regular ScriptAlias directive within a <LocationMatch> container if you prefer than method.

A warning: Your ErrorDocument directives are probably mis-configured; As written, they will return a 302-Found server response code for 403 and 404 errors, because they contain canonical URLs. This can wreak havoc with your search results.

The correct syntax is:


ErrorDocument 403 /somescript
ErrorDocument 404 /somescript

using a local URL-path only.

You can verify this problem using the Server Header Checker [webmasterworld.com] by requesting a non-existent page and a page to which you have denied Web access, such as an .htaccess or .htpasswd file. Instead of the desired 403-Forbidden or 404-Not Found response, you'll see a 302-Found redirect response.

See the Apache ErrorDocument documentation [httpd.apache.org] for an explanation. If you have seen strange and "non-optimal" search results and spider behaviour on your site, this is a likely cause.

Jim

rsgalloway

12:40 am on Sep 30, 2005 (gmt 0)

10+ Year Member



jdMorgan, thanks!

This is almost working. The only problem now is it's loading the default Apache page, not "index.hmtl" or even "home" as I would expect.

I added:

DirectoryIndex index.html

just to be sure. This is what I have now:

ScriptAliasMatch ^/([^/.]*)$ "/home/foo.com/$1"
ErrorLog /home/foo.com/error.log
LogLevel info
<Directory "/home/foo.com">
allow from all
Order allow,deny
</Directory>
ErrorDocument 403 /home
ErrorDocument 404 /home
DirectoryIndex index.html

Going to

[foo.com...]

gives me the Apache test page. adding any random chars after the /, eg.

[foo.com...]

brings up "home". But putting in an existing script file results in the same thing:

[foo.com...]

give me the same page, not "search". so something is definitely broken still.

Thanks for the help.

jdMorgan

1:30 am on Sep 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you can try this:

ScriptAliasMatch ^(/[^/.]*)?$ /home/foo.com$1

Maybe that will work better for the no-trailing-slash-on-domain case.

This is not something I've had to do on one of my sites, so be advised that I'm posting on a "theoretical" basis here. I also can't be sure that the path is correct, since I'm not looking at your directory-tree on my screen here.

Jim

rsgalloway

2:00 am on Sep 30, 2005 (gmt 0)

10+ Year Member




Thanks again, Jim. I'm not very good at regex scripting.

My directory tree looks like this:

/home/foo.com
/home/foo.com/images
/home/foo.com/bar

I have my vhosts setup so that www.foo.com points to

/home/foo.com

and bar.foo.com points to

/home/foo.com/bar

I need extensionless scripts in this dir and in /home/foo.com/bar to execute, but not html and image files (basically, no extension = script, extension = static file).

It would be great if index.html is the index file, but right now it's pulling up the default Apache page.

I have a script "search" in foo.com which when I load

www.foo.com/search?x=1

I get "file /search not found". So I guess your regex needs to be modified?

Thanks!

rsgalloway

2:23 am on Sep 30, 2005 (gmt 0)

10+ Year Member




update:

almost everything is working perfectly now, except this:

[foo.com...]

does not pull up index.html, instead it shows the Apache default page. I added in

DirectoryIndex index.html

and

Options +Indexes

but that didn't fix it. I am getting this in the error log:

attempt to invoke directory as script: /home/foo.com/

Any ideas?

Thanks.

jdMorgan

3:35 am on Sep 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What version of Apache are you on?

If Apache 1.x, make sure that the following modules are loaded in this order:
mod_cgi
mod_alias
mod_dir

These modules will be executed in *reverse order* of their appearance in the LoadModule list; mod_dir is responsible for adding missing trailing slashes, mod_alias is needed to process ScriptAliasMatch, and finally, mod_cgi is responsible for executing scripts. So they need to run in that order, and to be loaded in the reverse of that order. (Disregard any intervening modules loaded in the list, just make sure the modules named above are in that relative order.)

If you're on Apache 2.x, this does not apply, since execution priority is no longer controlled by the LoadModule directive, and module prioritizing is built-in.

If the modules are already correctly-ordered, and for some reason mod_dir isn't doing it's job, then we may have to add code to append a missing trailing slash manually. I'd rather not do that, since this code would have to be processed for every single request to the server -- No use wasting CPU if it's not absolutely required.

BTW, if you need to edit the distribution part of httpd.conf -- the part of the file that was there by default after installing the server, then please make a backup. If I'm wrong about something -- or mis-type something, I will feel much better if you have a backup... :)

Jim

jdMorgan

4:01 am on Sep 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One more thought:

I'm not sure what you changed between messages #5 and #6 above, but a request for "/" is definitely an extensionless request, and so the code I wrote will indeed try to run your script. To change that, the regex would need to be:


ScriptAliasMatch ^(/[^/.]+)$ /home/foo.com$1

but this is going to require a trailing slash to be used for "index page" requests of the form "example.com/", unless mod_dir does the trailing-slash fix-up as it should.

That's the nature of regex: It does exactly what you say, but often not what you want. ;)

Jim

rsgalloway

4:28 am on Sep 30, 2005 (gmt 0)

10+ Year Member



Jim, you are a genius!

It is working, finally! Whew. What a relief. I can get back to development.

FYI-

I am using Apache 2.x and mod_dir is definitely appending the trailing "/". I never knew how that happened, now I know.

This is what I had:

ScriptAliasMatch ^(/[^/.]*)?$ /home/foo.com$1

And I changed it to this just now:

ScriptAliasMatch ^(/[^/.]*)$ /home/foo.com$1

And it's all good! Thank you!

I guess I need to learn how to write regex.