Mapping subdomains to modules in Zend Framework

For some reason this took me a long time to figure out, so I thought I’d post it in case other people are having similar problems.  I wanted www.mysite.com to route to the www module and app.mysite.com to route to the app module.  In the end it was quite simple—I was trying to make it much more complicated than it needed to.

I’m using Zend Framework 1.8.4 with Zend_Application

Here’s my router configuration:

resources.router.routes.www.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.www.route = ":module.mysite.com"
resources.router.routes.www.defaults.module = "www"
resources.router.routes.www.chains.index.type = "Zend_Controller_Router_Route"
resources.router.routes.www.chains.index.route = ":controller/:action/*"
resources.router.routes.www.chains.index.defaults.controller = "index"
resources.router.routes.www.chains.index.defaults.action = "index"

And here’s my .htaccess rule that redirect to www if another subdomain is used (or no subdomain is given):

RewriteCond %{HTTP_HOST} !^(www|app)\.mysite\.(com|local)
RewriteRule ^(.*)$ http://www.mysite.%2/$1 [R=302,QSA,L]

Otherwise it’s just your standard modular structure generated with the zf tool.

Related posts:

  1. Installing Zend Server & Zend Framework on OS X This post is mostly a reminder to myself, but I...
  2. Review: Zend Framework 1.8 Web Application Development At the beginning of February PACKT Publishing sent me a...
  3. PHP Modeling (in Zend Framework) I’ve been thinking a lot about Modeling in a MVC...
  4. Zend Framework: Using separate layouts per module Someone was recently asking on ZFTalk about how to use...
  5. Zend Framework Bash Completion Script If you use the Zend Framework CLI interface much you...
This entry was posted in Web Development and tagged . Bookmark the permalink.

One Response to Mapping subdomains to modules in Zend Framework

  1. Thank you! your a legend. Though I haven’t tried it yet, it shall be a help!

blog comments powered by Disqus