Using Mod_rewrite To Hide The .py Extension Of A Python Script Accessed On A Web Browser
Solution 1:
If you are running on a linux box, dont use mod-rewrite - rename the script.
you can call the script - pythonscript not pythonscript.py
you add to the first line of the script pointing to your python interpreter
and set the file to be executable
with
chmod +x pythonscript
when the file is executed - it will read the first line of the file and execute the interpeter in the first line
#!/usr/bin/python
and then set the directory to execute the script
to make this work, you change the files in the directory executable in your .htaccess file
basics/.htaccess
-----------
Options +ExecCGI
SetHandler cgi-script
-----------
Your python script will look like this
basics/pythonscript
-------#!/usr/bin/python
print "STATUS: 200 OK\n\n"
print "hello world"
------
Warning... You may get an error in your error_log file that looks like this.
[Thu Aug 05 19:26:34 2010][alert][client 127.0.0.1] /home/websites/testing/htdocs/basics/.htaccess: Option ExecCGI not allowed here
If that happens your webserver is not allowing the changes from your .htaccess file
Your webserver will need to be able to allow changes in your htaccess file so you may need to enable Allowoverride All in your httpd.conf file
<Directory"/">
....
Allowoverride All
</Directory>
Solution 2:
I found the solution: I needed to add the application MIME type so that the Apache Web Server understands it should run the code.
In url.dev/basics/.htaccess add:
<IfModulemime_module>
AddType application/x-httpd-py .py
</IfModule>
Don't forget to restart Apache: sudo apachectl graceful
And it works! http://url.dev/basics/pythonscript runs the code in pythonscript.py! Note that I'm not sure if this is the best way to do it, but it works.
Here is my complete .htaccess file:
# Options +ExecCGI# SetHandler cgi-script
<IfModule mime_module>
AddType application/x-httpd-py .py
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /basics/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.py -f
RewriteRule ^(.*)$ $1.py
</IfModule>
Post a Comment for "Using Mod_rewrite To Hide The .py Extension Of A Python Script Accessed On A Web Browser"