如何从我的网址中隐藏“cgi-bin”,“。py”等?

时间:2020-11-27 23:09:01

Brand new to web design, using python. Got Apache up and running, test python script working in cgi-bin directory. Get valid results when I type in the URL explicitly: ".../cgi-bin/showenv.py"

全新的网页设计,使用python。启动并运行Apache,测试在cgi-bin目录下运行的python脚本。当我明确输入URL时获得有效结果:“... / cgi-bin / showenv.py”

But I don't want the URL to look that way. Here at *, for example, the URLs that display in my address bar never have the messy details showing the script that was used to run them. They're clean of cgi-bin, .py, etc. extensions. How do I do that?

但我不希望URL看起来那样。例如,在*中,显示在我的地址栏中的URL永远不会有杂乱的细节显示用于运行它们的脚本。他们没有cgi-bin,.py等扩展。我怎么做?

EDIT: Thanks for responses, every single one helpful, lots to learn. I'm going with URL Rewriting for now; example in the docs looks extremely close to what I actually want to do. But I'm committed to python, so will have to look at WSGI down the road.

编辑:感谢您的回复,每一个都很有帮助,很多东西需要学习。我现在正在使用URL重写;文档中的示例看起来非常接近我实际想要做的事情。但是我致力于python,因此将不得不关注WSGI。

6 个解决方案

#1


The python way of writing web applications is not cgi-bin. It is by using WSGI.

编写Web应用程序的python方式不是cgi-bin。它是通过使用WSGI。

WSGI is a standard interface between web servers and Python web applications or frameworks. The PEP 0333 defines it.

WSGI是Web服务器和Python Web应用程序或框架之间的标准接口。 PEP 0333对其进行了定义。

There are no disadvantages in using it instead of CGI. And you'll gain a lot. Beautiful URLs is just one of the neat things you can do easily.

使用它而不是CGI没有任何缺点。而且你会获得很多。美丽的网址只是您可以轻松完成的一件巧妙的事情。

Also, writing a WSGI application means you can deploy on any web server that supports the WSGI interface. Apache does so by using mod_wsgi.

此外,编写WSGI应用程序意味着您可以在支持WSGI接口的任何Web服务器上进行部署。 Apache通过使用mod_wsgi来实现。

You can configure it in apache like that:

您可以在apache中配置它:

WSGIScriptAlias /myapp /usr/local/www/wsgi-scripts/myapp.py

Then all requests on http://myserver.domain/myapp will go to myapp.py's application callable, including http://myserver.domain/myapp/something/here.

然后,http://myserver.domain/myapp上的所有请求都将转到myapp.py的应用程序可调用,包括http://myserver.domain/myapp/something/here。

example myapp.py:

def application(environ, start_response):
    start_response('200 OK', [('Content-type', 'text/plain')])
    return ['Hello World!']

#2


I think you can do this by rewriting URL through Apache configuration. You can see the Apache documentation for rewriting here.

我认为你可以通过Apache配置重写URL来实现这一点。您可以在此处查看Apache文档以进行重写。

#3


You have to use URL Rewriting.

您必须使用URL重写。

It is not a noob question, it can be quite tricky :)

这不是一个菜鸟问题,它可能相当棘手:)

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Hope you find it helpful

希望你觉得它有用

#4


Just use some good web framework e.g. django and you can have such URLs more than URLs you will have a better infrastructure, templates, db orm etc

只需使用一些好的Web框架,例如django,你可以拥有比URL更多的URL,你将拥有更好的基础设施,模板,数据库等

#5


this is an excerpt from a .htaccess that I use to achieve such a thing, this for example redirects all requests that were not to index.php to that file, of course you then have to check the server-variables within the file you redirect to to see, what was requested.

这是我用来实现这种事情的.htaccess的摘录,这例如将所有不是index.php的请求重定向到该文件,当然你必须检查你重定向的文件中的服务器变量要看,请求了什么。

Or you simply make a rewrite rule, where you use a RegExp like ^.*\/cgi-bin\/.*\.py$ to determine when and what to rewrite. Such a RegExp must be crafted very carefully, so that rewriting only takes place when desired.

或者你只是制作一个重写规则,你可以使用像^。* \ / cgi-bin \ /.* \。py $这样的RegExp来确定重写的时间和内容。必须非常仔细地制作这样的RegExp,以便只在需要时进行重写。

<IfModule mod_rewrite.c>
    RewriteEngine On   #activate rewriting
    RewriteBase /      #url base for rewriting
    RewriteCond %{REQUEST_FILENAME} !index.php #requested file is not index.php
    RewriteCond %{REQUEST_FILENAME} !^.*\.gif$ #requested file is no .gif
    RewriteCond %{REQUEST_FILENAME} !^.*\.jpg$ #requested file is no .jpg
    RewriteCond %{REQUEST_FILENAME} !-d        #is not a directory
    RewriteRule . /index.php [L]               #send it all to index.php
</IfModule>

The above Example uses RewriteConditions to determine when to rewrite ( .gif's, .jpeg's and index.php are excluded ).

上面的例子使用RewriteConditions来确定何时重写(.gif,.jpeg和index.php被排除在外)。

Hmm, so thats a long text already. Hope it was a bit helpful, but you won't be able to avoid learning the syntax of the Apache RewriteEngine.

嗯,这已经很长了。希望它有点帮助,但你将无法避免学习Apache RewriteEngine的语法。

#6


You'll find the ScriptAlias directive helpful. Using

您会发现ScriptAlias指令很有用。运用

ScriptAlias /urlpath /your/cgi-bin/script.py

you can access your script via http://yourserver/urlpath.

您可以通过http:// yourserver / urlpath访问您的脚本。

You also might want to look into mod_passenger, though the last time I used it, WSGI was kind of a "second-class citizen" within the library—it could detect WSGI scripts if it were used to serve the whole domain, but otherwise there are no directives to get it to run a WSGI app.

您也可能想查看mod_passenger,虽然上次我使用它时,WSGI在库中是一种“二等公民” - 它可以检测WSGI脚本,如果它用于服务整个域,但除此之外没有指令让它运行WSGI应用程序。

#1


The python way of writing web applications is not cgi-bin. It is by using WSGI.

编写Web应用程序的python方式不是cgi-bin。它是通过使用WSGI。

WSGI is a standard interface between web servers and Python web applications or frameworks. The PEP 0333 defines it.

WSGI是Web服务器和Python Web应用程序或框架之间的标准接口。 PEP 0333对其进行了定义。

There are no disadvantages in using it instead of CGI. And you'll gain a lot. Beautiful URLs is just one of the neat things you can do easily.

使用它而不是CGI没有任何缺点。而且你会获得很多。美丽的网址只是您可以轻松完成的一件巧妙的事情。

Also, writing a WSGI application means you can deploy on any web server that supports the WSGI interface. Apache does so by using mod_wsgi.

此外,编写WSGI应用程序意味着您可以在支持WSGI接口的任何Web服务器上进行部署。 Apache通过使用mod_wsgi来实现。

You can configure it in apache like that:

您可以在apache中配置它:

WSGIScriptAlias /myapp /usr/local/www/wsgi-scripts/myapp.py

Then all requests on http://myserver.domain/myapp will go to myapp.py's application callable, including http://myserver.domain/myapp/something/here.

然后,http://myserver.domain/myapp上的所有请求都将转到myapp.py的应用程序可调用,包括http://myserver.domain/myapp/something/here。

example myapp.py:

def application(environ, start_response):
    start_response('200 OK', [('Content-type', 'text/plain')])
    return ['Hello World!']

#2


I think you can do this by rewriting URL through Apache configuration. You can see the Apache documentation for rewriting here.

我认为你可以通过Apache配置重写URL来实现这一点。您可以在此处查看Apache文档以进行重写。

#3


You have to use URL Rewriting.

您必须使用URL重写。

It is not a noob question, it can be quite tricky :)

这不是一个菜鸟问题,它可能相当棘手:)

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Hope you find it helpful

希望你觉得它有用

#4


Just use some good web framework e.g. django and you can have such URLs more than URLs you will have a better infrastructure, templates, db orm etc

只需使用一些好的Web框架,例如django,你可以拥有比URL更多的URL,你将拥有更好的基础设施,模板,数据库等

#5


this is an excerpt from a .htaccess that I use to achieve such a thing, this for example redirects all requests that were not to index.php to that file, of course you then have to check the server-variables within the file you redirect to to see, what was requested.

这是我用来实现这种事情的.htaccess的摘录,这例如将所有不是index.php的请求重定向到该文件,当然你必须检查你重定向的文件中的服务器变量要看,请求了什么。

Or you simply make a rewrite rule, where you use a RegExp like ^.*\/cgi-bin\/.*\.py$ to determine when and what to rewrite. Such a RegExp must be crafted very carefully, so that rewriting only takes place when desired.

或者你只是制作一个重写规则,你可以使用像^。* \ / cgi-bin \ /.* \。py $这样的RegExp来确定重写的时间和内容。必须非常仔细地制作这样的RegExp,以便只在需要时进行重写。

<IfModule mod_rewrite.c>
    RewriteEngine On   #activate rewriting
    RewriteBase /      #url base for rewriting
    RewriteCond %{REQUEST_FILENAME} !index.php #requested file is not index.php
    RewriteCond %{REQUEST_FILENAME} !^.*\.gif$ #requested file is no .gif
    RewriteCond %{REQUEST_FILENAME} !^.*\.jpg$ #requested file is no .jpg
    RewriteCond %{REQUEST_FILENAME} !-d        #is not a directory
    RewriteRule . /index.php [L]               #send it all to index.php
</IfModule>

The above Example uses RewriteConditions to determine when to rewrite ( .gif's, .jpeg's and index.php are excluded ).

上面的例子使用RewriteConditions来确定何时重写(.gif,.jpeg和index.php被排除在外)。

Hmm, so thats a long text already. Hope it was a bit helpful, but you won't be able to avoid learning the syntax of the Apache RewriteEngine.

嗯,这已经很长了。希望它有点帮助,但你将无法避免学习Apache RewriteEngine的语法。

#6


You'll find the ScriptAlias directive helpful. Using

您会发现ScriptAlias指令很有用。运用

ScriptAlias /urlpath /your/cgi-bin/script.py

you can access your script via http://yourserver/urlpath.

您可以通过http:// yourserver / urlpath访问您的脚本。

You also might want to look into mod_passenger, though the last time I used it, WSGI was kind of a "second-class citizen" within the library—it could detect WSGI scripts if it were used to serve the whole domain, but otherwise there are no directives to get it to run a WSGI app.

您也可能想查看mod_passenger,虽然上次我使用它时,WSGI在库中是一种“二等公民” - 它可以检测WSGI脚本,如果它用于服务整个域,但除此之外没有指令让它运行WSGI应用程序。