使用mod_rewrite隐藏在Web浏览器访问一个Python脚本的.py扩展名 [英] Using mod_rewrite to hide the .py extension of a Python script accessed on a web browser
问题描述
我想隐藏在Web浏览器加载一个Python脚本的.py扩展名,仍然有脚本运行。例如:在地址栏火灾pythonscript.py键入 url.dev/basics/pythonscript
,并显示在浏览器窗口中的结果。
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.
- 的URL url.dev/basics/pythonscript获取静态文件/pythonscript.py
- 浏览器仍然显示的URL url.dev/basics//pythonscript
在 url.dev/basics/pythonscript.py
键入不工作,并显示Python脚本结果。我还可以得到mod_rewrite的重写 url.dev/basics/phpscript
到 url.dev/basics/phpscript.php
并成功运行PHP的code幕后。
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.
但 url.dev/basics/pythonscript
不重定向到 url.dev/basics/pythonscript.py
(我得到一个404未找到)
背景信息
A) PHP改写的作品:在位于url.dev/basics/一个.htaccess下面的PHP脚本如下:
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重写不起作用:在位于url.dev/basics/一个.htaccess下列不为Python脚本的工作(我得到一个404未找到)
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)我是编程新手通过工作的练习2:您的第一个程序中的互联网应用软件工程基础部分。我试图按照建议使用执行环境,一个URL =一个文件',但要使用Python而不是PHP。的我知道这是不是构建Web应用程序的路线的最佳途径。它只是上面链接的过程的初始部分期间使用的约定。的
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)我在操作系统设置了虚拟主机开发环境10.6雪豹,这样我可以在adactio.com在url.dev按照哈克假期访问我的发展。我的Python版本是2.6.1的Python
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)我计划最终使用Django,但要首先如果可能的话在工作简单code这样我就可以更好地理解正在发生的事情。
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)我在我的httpd.conf以下内容:
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
和
LoadModule php5_module libexec/apache2/libphp5.so
LoadModule fastcgi_module libexec/apache2/mod_fastcgi.so
G)我的Apache的版本(看到服务器日志重新启动服务器后):的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配置 - 恢复正常运作的
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
期待任何帮助!
推荐答案
我找到了解决办法。我需要添加应用程序的MIME类型,以便在Apache Web服务器懂得它应该运行code
I found the solution: I needed to add the application MIME type so that the Apache Web Server understands it should run the code.
在url.dev/basics/.htaccess地址:
In url.dev/basics/.htaccess add:
<IfModule mime_module>
AddType application/x-httpd-py .py
</IfModule>
不要忘了重新启动Apache:须藤apachectl中曼妙
和它的作品! http://url.dev/basics/pythonscript 运行code在pythonscript.py!请注意,我的不的知道这是做的最好的方式,但它的作品。
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.
下面是我的完整.htaccess文件:
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>
这篇关于使用mod_rewrite隐藏在Web浏览器访问一个Python脚本的.py扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!