How to hide “cgi-bin”, “.py”, etc from my URLs?

淺唱寂寞╮ 提交于 2019-12-02 18:29:07

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

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

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.

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.

You can configure it in apache like that:

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.

example myapp.py:

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

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

You have to use URL Rewriting.

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

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

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.

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.

<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 ).

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.

You'll find the ScriptAlias directive helpful. Using

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

you can access your script via 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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!