PHP Shindig and mod_rewrite

This is something that some people run into, they want to try out PHP shindig but it just 'doesn't work'; This problem is also known as 'where is my /gadgets/ifr file?'.

The PHP version of Shindig uses something called 'mod_rewrite' to do url => action mapping, and on some hosts this isn't enabled by default. Click the more link to read how to enable this on your apache server.

You'll notice that in shindig/php there is a file called .htaccess that contains:

CODE:
  1. <IfModule mod_rewrite.c>
  2. RewriteEngine On
  3. RewriteCond %{REQUEST_FILENAME} !-f
  4. RewriteCond %{REQUEST_FILENAME} !-d
  5. RewriteRule (.*) index.php [L]
  6. </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:

CODE:
  1. $servletMap = array(
  2. Config::get('web_prefix') . '/gadgets/files' => 'FilesServlet',
  3. Config::get('web_prefix') . '/gadgets/js' => 'JsServlet',
  4. Config::get('web_prefix') . '/gadgets/proxy' => 'ProxyServlet',
  5. Config::get('web_prefix') . '/gadgets/makeRequest' => 'ProxyServlet',
  6. Config::get('web_prefix') . '/gadgets/ifr' => 'GadgetRenderingServlet',
  7. Config::get('web_prefix') . '/gadgets/metadata' => 'JsonRpcServlet',
  8. Config::get('web_prefix') . '/social/data' => 'GadgetDataServlet',
  9. Config::get('web_prefix') . '/social/rest' => 'RestServlet',
  10. Config::get('web_prefix') . '/public.crt' => 'CertServlet'
  11. );

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 :

CODE:
  1. <VirtualHost your_ip:your_port>
  2. ServerName your.host
  3. DocumentRoot /var/www/html/shindig/php
  4. ... other normal settings in vhosts...
  5. <Directory />
  6.     AllowOverride All
  7. </Directory>
  8. </VirtualHost>

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

CODE:
  1. LoadModule rewrite_module modules/mod_rewrite.so

if it's

CODE:
  1. #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 :-)

OpenSocial, 1 comments,

One Response to “PHP Shindig and mod_rewrite”

  1. Ben Clemens Says:

    Something obvious that I kept overlooking: if you are hosting shindig and partuza on a server (i.e. not a laptop) with a virtual host the paths to the controllers might get screwed up if the server’s conf is something other than the default (for example, if other rewrite rules are in effect). In my case this was fixed by adding a RewriteBase directive to the .htaccess file (e.g. RewriteBase /partuza/html). If you suffer from a blindness to the obvious as I do, it might help to try…


Leave a Reply