I’ve been a little busy for the last few months. As I hinted at a couple of times, I’ve been working on a new venture. Well, that venture is starting to get more public. My partner and I have been working on Inquisix. Inquisix is a site designed for sales people to help them meet better prospects. I’m having a blast, and it’s great to be able to talk about things more.
Anyway, the interesting technical item that came up is related to our blog. At my last startup, I migrated my team to Ruby from Java, and I haven’t looked back. That made Ruby on Rails an easy choice as a development platform for Inquisix. Of course, we also wanted a blog. There are Ruby alternatives for blogging. Mephisto is probably the best example, but I am more familiar with WordPress. While it would be great to have a Ruby blog, it’s hard to ignore WordPress for blogging, and I’m not a big enough Ruby zealot to require that everything must be Ruby. Therefore, I decided on WordPress.
However, there is one problem. I wanted my blog’s url to be http://inquisix.com/blog. How do I convince PHP and Rails to co-exist on the same server? After a little searching, I found a great blog post by Ilya Grigorik – Integrating WordPress and Rails. At first, it worked great, but things failed when I tried to change my settings to use permalinks based on dates. Once I did this, only pages with an existing physical file worked.
This is what Ilya proposed for a local installation of WordPress:
# Check for maintenance file and redirect all requests RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f RewriteCond %{SCRIPT_FILENAME} !maintenance.html RewriteRule ^.*$ /system/maintenance.html [L
# Let apache handle the PHP files - all requests that get past this rule # are routed to the mongrel cluster (aka Rails # - wordpress installation assumeed to be in 'public/wordpress' # - Options: NC - case insensitive # - QSA - query string append # - L - last rule, aka stop here if rewriterule condition is matched RewriteRule ^/blog/?(.*)$ %{DOCUMENT_ROOT}/wordpress/$1 [NC,QSA,L]
# Redirect all non-static requests to cluster RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
This is what I ended up with:
# Check for maintenance file and redirect all requests RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f RewriteCond %{SCRIPT_FILENAME} !maintenance.html RewriteRule ^.*$ /system/maintenance.html [L]
# Let apache handle the PHP files RewriteCond /opt/apps%{REQUEST_FILENAME} !-f RewriteCond /opt/apps%{REQUEST_FILENAME} !-d RewriteRule ^/blog/?.*$ /opt/apps/blog/index.php [NC,QSA,L] RewriteRule ^/blog/?(.*)$ /opt/apps/blog/$1 [NC,QSA,L] # Rewrite rule for Rails static assets RewriteRule "^/(images|stylesheets|javascripts)/?(.*)" "$0" [NC,QSA,L] # Rewrite index to check for static RewriteRule ^/$ /index.html [QSA] # Rewrite to check for Rails cached page RewriteRule ^([^.]+)$ $1.html [QSA] # Redirect all non-static requests to cluster RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
The problem with Ilya’s is that it rewrites everything to a physical file, but when WordPress is setup to use slugs, this will fail because the physical file doesn’t exist. If the file doesn’t exist, you need to rewrite the url to index.php.
For grins, I also added a line to let Apache handle Rails cached files, images, stylesheets, and javascripts because Mongrel is not that great at delivering static assets.
I’m still a little new to Apache configs, so if someone has a better way to handle this, I’m all ears.
Leave a Reply