views:

661

answers:

6

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.

+4  A: 

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

Valentin Rocher
+4  A: 

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

The real napster
+5  A: 

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

Anurag Uniyal
That is not always a good idea.If you are making something that is truely custom you will always be restrained by the borders and rules of the framework you choose.
The real napster
yes right, but not sure if he want to develop web framework of his own
Anurag Uniyal
Use a framework -- you'll be much, much happier than inventing your own framework wrapped around cgi-bin.
S.Lott
django and I will meet on some future date. But for now I need to learn the nuts and bolts of web construction. Want to understand it from the ground up.
John Pirie
@John Pine: Noble, but... You're likely to understand more based on learning a framework's solutions than by inventing your own solutions. Inventing a not-quite-so-good solution may make the framework's very clever solution seem like "overkill" or "confusing".
S.Lott
+1  A: 

Hi there,

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.

Paul
+8  A: 

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!']
nosklo
+1 (more if I could). If you want to understand Python web development "from the ground up," you should still ignore CGI entirely. Writing your own bare-WSGI app is really easy, and a good way to get a handle on what the Python frameworks are wrapping for you.
Carl Meyer
A: 

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.

Paul Fisher