Search code examples
phppythonapache2webapp2

webapp2 behaviour replicated in apache/php


In webapp2 (python) you can map urls to classes in one file.

app = webapp2.WSGIApplication([('/', pgMain), ('/test/', pgTest)], debug=True)

How would I setup such a thing in php & apache?

Thanks

EDIT:

What I am after is everything after a foldername to be redirected to a file in that folder. eg.

/version/1.0/test/folder/ => /version/1.0/index.php & var="/test/folder"
/version/1.0/another/     => /version/1.0/index.php & var="/another"

Thanks.


Solution

  • Simplest way for doing this is to create .htaccess file in your app folder and put this code there:

    RewriteEngine On
    RewriteBase /
    
    RewriteRule "(^|/)\." - [F]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !=/favicon.ico
    RewriteRule ^ index.php [NC,NS,L]
    

    Please note that you need to modify "RewriteBase" directive and put path of your application there "/" means your document root.

    After creating .htacess file all requests will be routed to your index.php file and you can access URI via PHP's super global variable $_SERVER['REQUEST_URI']