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

后端 未结 6 1968
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 08:49

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: \".../

相关标签:
6条回答
  • 2021-02-01 09:30

    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.

    0 讨论(0)
  • 2021-02-01 09:36

    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

    0 讨论(0)
  • 2021-02-01 09:37

    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

    0 讨论(0)
  • 2021-02-01 09:38

    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!']
    
    0 讨论(0)
  • 2021-02-01 09:43

    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.

    0 讨论(0)
  • 2021-02-01 09:49

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

    0 讨论(0)
提交回复
热议问题