Rails + Wordpress + Apache integration
Last week in my work I found with a requirement that took me more time than I expected to solve. We had a wordpress blog running under a subdomain of our main app. Let’s say blog.myapp.com and we wanted to move it to myapp.com/blog. Should be simple right?.So I started to do some research and I found this post by Ilya Grigorik. I also read this post by Barry Hess that helped me a lot.
I found others that contributed to my solution, but none of them worked for me at the first try. What I did at the end was a combination of rewrite conditions inside wordpress .httaccess file and the use of alias and location inside the VirtualHost configuration.
Configuration Steps
The first thing that you need to do is to add the following to the virtualhost configuration of your main site:
Alias /blog /path/to/wordpress/blog <Directory /path/to/wordpress/blog> Allow From all Options +Indexes AllowOverride all </Directory> <Location /blog> PassengerEnabled off </Location>
After this, you should go to /path/to/wordpress and edit the .htaccess and change it to:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
Now there is an important step that you need to do inside the wordpress application. You must change the URL of the app to point to the new direction. I did this defining two variables in the wp-config.php file:
define('WP_HOME','http://myapp.com/blog');
define('WP_SITEURL','http://myapp.com/blog');
The previous step you could do it trough the wordpress settings UI as well if you want
So that should be it, you just need to restart your apache server and your blog is going to be running and working under myapp.com/blog
Corollary
If you don’t want to lost traffic from previous urls pointing to blog.myapp.com you could add the following rewrite rule in the virtual host configuration the app:... ServerName blog.myapp.com DocumentRoot /path/to/wordpress RewriteEngine on RewriteRule ^(.*) http://www. myapp.com/blog$1 [R=301,L] ...