Update: I have written a tool than can generate automatically the proper configuration and much more!
I’ve seen some articles on how to configure lighttpd to serve a Symfony project, however they usually did at least one mistake:
- Assuming that requests with periods (‘
.
‘) are for static files (the period is a default separator in Symfony, and is extensively used in the new admin generators). - Ignoring parameters after a ‘
?
‘ (they are not widely used, except… in the new admin generators, and can be very useful if your application)
For the first part, there is a much simpler solution to handle static files: most of them are in specific directories, except for a very limited number of ones.
My solution also handles assets published by plugins (you might want to edit the corresponding line to a more liberal one though).
You might want to add your sitemap.xml.gz
or robots.txt
to this list if you generate them statically.
For the second part, you simply have to match explicitly the ‘?
‘ part.
Here is the magic:
alias.url = ( "/sf/" => "/home/web/symfony_12/data/web/sf/" ) url.rewrite-once = ( "^/css/.+" => "$0", # directories with static files "^/js/.+" => "$0", "^/images/.+" => "$0", "^/uploads/.+" => "$0", "^/favicon\.ico$" => "$0", # static file example "^/sf[A-z]+Plugin.*" => "$0", # plugins "^/sf/.+" => "$0", # symfony assets "^/backend\.php(/[^\?]*)(\?.*)?" => "/backend.php$1$2", # allow access to another application "^(/[^\?]*)(\?.*)?" => "/index.php$1$2" # default application ) |
I guess the usage of periods in the rules also had the benefit of allowing the access to any alternative application automatically. With my solution you have to add each appname.php
file manually, unless you use:
"^/([a-z]+)\.php(/[^\?]*)(\?.*)?" => "/$1.php$2$3", # any app (prod) |
Or for allowing any environment:
"^/([a-z_]+)\.php(/[^\?]*)(\?.*)?" => "/$1.php$2$3", # any app (any env) |
Note: your application must contain only lowercase letters, but you’re free to adapt it to your own usage.
5 Comments
I always felt like the absence of a “file exists” rule was a feature of lighttpd. I don’t think it’s that a good idea . But it will make hosting some applications much easier; I’ve seen some horrific rewriting examples ;-).
Thanks for the post.
By the way, I just released a new plugin for dealing with domains and subdomains in an elegant way straight from the routing system… It’s called sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin) and it lets you do some fancy things with subdomains, or domains in an elegant way. The kind of things that most of us had to resort to url rewrite hacks for.
For example, you can restrict rules to certain domains or subdomains (e.g. http://www.site.com and blog.site.com will go to different routes in the same application) , or even pass parameters in the subdomain (e.g. username.site.com) or even in the domain (e.g. PS2News.com vs. XboxNews.com).
fastcgi.server
line inside of a$HTTP["url"]
conditional (I already have onefastcgi.server
line per$HTTP["host"]
and it works great).