I want to hide the .py extension of a Python script loaded in a web browser and still have the script run. For example: typing url.dev/basics/pythonscript
in the address bar fires pythonscript.py and shows the results in the browser window.
- The URL url.dev/basics/pythonscript fetches the static file /pythonscript.py
- The browser still displays the url url.dev/basics//pythonscript
Typing in url.dev/basics/pythonscript.py
DOES work and the Python script results is displayed. I can also get mod_rewrite to rewrite url.dev/basics/phpscript
to url.dev/basics/phpscript.php
and run the PHP code successfully behind the scenes.
But url.dev/basics/pythonscript
does NOT redirect to url.dev/basics/pythonscript.py
(I get a 404 Not Found).
Background Info
A) PHP rewriting works: the following in an .htaccess located in url.dev/basics/ WORKS for PHP scripts:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /basics/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
B) Python rewriting does NOT work: the following in an .htaccess located in url.dev/basics/ does NOT work for Python scripts (I get a 404 Not Found):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /basics/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.py -f
RewriteRule ^(.*)$ $1.py
</IfModule>
C) I am a beginning programmer working through Exercise 2: Your first program in the Basics section of Software Engineering for Internet Applications. I am trying to follow the recommendation to use an execution environment where 'One URL = one file', but want to use Python rather than PHP. I realize that this is not the best way to build a web application down the line. It is only a convention to be used during the initial part of the course linked above.
D) I set up the Virtual Hosts development environment in OS 10.6 Snow Leopard so that I can access my development at url.dev as per 'Hacky Holidays' at adactio.com. My Python version is Python 2.6.1.
E) I plan to use Django eventually, but want to work on simpler code first if possible so I can better understand what is going on.
F) I have the following in my httpd.conf:
TypesConfig /private/etc/apache2/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddHandler cgi-script .py
and:
LoadModule php5_module libexec/apache2/libphp5.so
LoadModule fastcgi_module libexec/apache2/mod_fastcgi.so
G) My Apache version (seen in server log after restarting the server): Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8l DAV/2 PHP/5.3.1 mod_fastcgi/2.4.2 configured -- resuming normal operations
Looking forward to any help!