GSoC2008modrewrite

From MWWiki

Jump to: navigation, search

Back to GSoC 2008

The PHP version uses something called 'mod_rewrite' to do url =>  
action mapping.

You'll notice that in shindig/php there is a file called .htaccess  
that contains:
<IfModule mod_rewrite.c>
         RewriteEngine On
         RewriteCond %{REQUEST_FILENAME} !-f
         RewriteCond %{REQUEST_FILENAME} !-d
         RewriteRule (.*) index.php [L]
</IfModule>

In other, more human, words what this does is that ANY url where it  
isn't a reference to an existing file or directory, it points the  
request too /index.php

When you then look in index.php you'll notice it has a url => class  
mapping:

$servletMap = array(
                 Config::get('web_prefix') . '/gadgets/files' =>  
'FilesServlet',
                 Config::get('web_prefix') . '/gadgets/js' =>  
'JsServlet',
                 Config::get('web_prefix') . '/gadgets/proxy' =>  
'ProxyServlet',
                 Config::get('web_prefix') . '/gadgets/makeRequest' =>  
'ProxyServlet',
                 Config::get('web_prefix') . '/gadgets/ifr' =>  
'GadgetRenderingServlet',
                 Config::get('web_prefix') . '/gadgets/metadata' =>  
'JsonRpcServlet',
                 Config::get('web_prefix') . '/social/data' =>  
'GadgetDataServlet',
                 Config::get('web_prefix') . '/social/rest' =>  
'RestServlet',
                 Config::get('web_prefix') . '/public.crt' =>  
'CertServlet'
                 );

So it matches, lets say /gadgets/ifr to the class  
'GadgetRenderingServlet', which it then instances and depending on the  
http method calls doGet / doPost / etc. From there on the event  
specific logic takes place.

This type of construct is quite a common and a popular solution to get  
pretty url's in PHP applications, since it's unseemly to always have  
to do /gadgets/ifr.php, and needing an actual gadgets directory, and a  
ifr.php file .. for simple programs that works, but for larger  
applications where you need a good source layout, this doesn't scale  
well.

A lot of hosting parties already have support for mod_rewrite  
and .htaccess files enables, since a lot of popular software, like for  
instance wordpress use this for their 'pretty urls' (ie have /category/
title-of-blog-post instead of /blog.php?id=12) so in most cases you  
don't have to do anything to make this work. However if it doesn't  
what you need to do is:

Edit your virtual host configuration and add (to enable .htaccess  
files to define apache behavior) an AllowOverride All, like :

         <VirtualHost your_ip:your_port>
                ServerName your.host
                DocumentRoot /var/www/html/shindig/php
                ... other normal settings in vhosts...
                 <Directory />
                         AllowOverride All
                 </Directory>
         </VirtualHost>

Also make sure that the mod_rewrite module is loaded, on most unix  
systems you'd look for a line like:

        LoadModule rewrite_module modules/mod_rewrite.so

if it's

        #LoadModule rewrite_module modules/mod_rewrite.so

instead, remove the # infront of it.

After making those change(s), restart apache, and you should be good  
to go :)

        -- Chris