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

╄→гoц情女王★ 提交于 2019-12-03 05:11:46

问题


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"

But I don't want the URL to look that way. Here at stackoverflow, 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?

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.


回答1:


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!']



回答2:


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




回答3:


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




回答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




回答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.

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.




回答6:


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.



来源:https://stackoverflow.com/questions/882430/how-to-hide-cgi-bin-py-etc-from-my-urls

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