Rails + WordPress (at the same time)

June 1st, 2010 by e-thang Leave a reply »

It is a Sin to be a “Rails programmer” and not use a blogging engine you wrote yourself and especially in Rails, but there are cases when one’s available time is more important than one’s soul.

This particular case assumes you use shared hosting.  That’s probably another bad thing, but whatever.  Also, you need access to your public_html folder’s .htaccess file.  In fact, this whole setup is so host-specific that you might find it worthless.

Starting over.  Shared host.  public_html folder. Apache. Your host routes traffic to your app by rewriting your requests via a proxy to a specific port.

Now let’s say you have the domain www.domain.com.  Your host does something like:

RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$

RewriteRule ^(.*)$ "http\:\/\/127\.0\.0\.1\:12%{REQUEST_URI}" [P,QSA,L]

Note: port numbers have been changed to protect the innocent.

This basically just says “if a request hits me and the host matches your domain, I’m gong to proxy it on to myself on a different port.”  The webserver runs a request against itself on a different port, which presumably is picked up by whatever handles your Rails application.

Well, you want your blog to be at http://www.domain.com/blog.  That unfortunately does contain your domain in the HTTP_HOST.  Which means Rails will try to serve up your blog.  Which means it will fail or, if you actually have your blog in your app’s public folder, will serve up your .php files as raw text downloads.


RewriteCond %{REQUEST_URI} !^/blog/(.*)$
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^(.*)$ "http\:\/\/127\.0\.0\.1\:12%{REQUEST_URI}" [P,QSA,L]

We add an additional condition to our rewrite requiring that the actual path requested (Apache’s REQUEST_URI expands to that) NOT match “/blog/” followed by pretty much anything.  This will get your webserver to handle all of these requests as if your Rails app weren’t there, which is what you’re going for anyway.

This isn’t limited to WordPress.  And note that whatever you are trying to run needs to be in your Apache DOCUMENT_ROOT folder.  That could be like “public_html” or something.  I don’t know the answer to that for you.  You CAN work around that with other directives, but that is beyond the scope of this entry.


Leave a Reply