I’m using the Zend Framework to create an API
I want to use apache mod_rewrite to change some of the URI’s a bit,
but I’m still in the n00b level of apache rewite’s
currently I’m using the default ZF .htaccess file to remove the index.php file and pretty the URI’s:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
currently the URI’s looks like this:
http://domain.com/module/index/action/id/5/format/json
What I would like to do:
- If the controller (after /module) equals “index” it should be removed from the URI.
- If there is an int after action/ the “id” param should be removed and only pass the value.
- And finally I would like the to append the format as a suffix to the end of URI e.g. …/action/5.json
So that when I get the following URI:
http://domain.com/module/action/5.json
it will still points to:
http://domain.com/module/index/action/id/5/format/json
Is it possible to do all of this in one statement?
or would it be a better option to add a custom route in the ZF Bootstrap?
Typically in a Zend Framework application, the only url rewriting that occurs using Apache’s
mod_rewriteat the.htaccesslevel is the rewriting you have in the question. This pushes all requests through the framework which then has it’s own internal mechanism for handling the request.This internal process of mapping a url to a (module, controller, action) triple and setting request parameters is called “routing”. Zend Framework allows you to define custom routes.
Typically, routes are defined at the bootstrapping stage; after all, it has to be done early in the process so the system can determine which controller needs to be instantiated to handle the request.
The ZF documentation for routing describes it pretty well.
In summary: Best to do this via custom route defined within the application.