Wordpress blog for Ruby On Rails, the configuration that worked for me
By: Med Bouzid
Tags: Ruby on Rails, Blog, WordPress

I spent some considerable time and effort trying to figure out how to make Wordpress working with my Rails app using a reverse proxy… if you are in hurry just read the short explanation, otherwise you can read the longest one (which is the same as the short one but with few more boring details):

Short explanation:

have your blog installed on some server and accessible through some subdomain, for example https://blog.example.com add reverse proxy gem to your application Gemfile

gem 'rack-reverse-proxy', :require => 'rack/reverse_proxy'

3. in your config/application.rb add the following (make sure to replace http://blog.example.com by your blog URL, also note that there is a $1 right after the .com — NO slash between the two)



reverse_proxy_options preserve_host: true

reverse_proxy /^\/blog(\/.*)$/, '

end config.middleware.insert(0, Rack::ReverseProxy) doreverse_proxy_options preserve_host: truereverse_proxy /^\/blog(\/.*)$/, ' http://blog.example.com$1' , :username => 'basic-auth-username', :password => 'basic-auth-password',:timeout => 500end

Note: username and password above are useful for step (7)

4. In your routes.rb add

5. Now log-in to your blog admin area and set WordPress Address (URL) and Site Address (URL) options to your Rails app url + /blog as follows:

6. in your wordpress server, add the following to wp-config.php at the VERY TOP (right after <?php):

define('FORCE_SSL_ADMIN', true); if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')

$_SERVER['HTTPS']='on'; if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {

$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];

} // NOTE: ===> if your Wordpress is installed physically under a /blog folder in your server, then the line below should be omitted. $_SERVER['REQUEST_URI'] = '/blog'.$_SERVER['REQUEST_URI'];

6.1) If you are going to use a Static Page as your home page instead of the default one (which is “Your latest posts”) you may encounter the problem “The page isn’t redirecting properly”

The “hack” I did to solve this is replacing the line

$_SERVER['REQUEST_URI'] = '/blog'.$_SERVER['REQUEST_URI'];

by

// Prepend '/blog' to REQUEST_URI only if it's different than '/' to prevent "The page isn’t redirecting properly" issue: if($_SERVER['REQUEST_URI'] !== '/'){



$_SERVER['REQUEST_URI'] = '/blog'.$_SERVER['REQUEST_URI'];

} // some theme builders add query string to edit url, which causes the home page (/) to have an incorrect pattern (/?) that leads to page redirection issue (when you click edit with builder)! the following line will fix it: $_SERVER['REQUEST_URI'] = str_replace('/?', '?', $_SERVER['REQUEST_URI']);

If your wordpress is physically installed on a “/blog” folder instead of the root of server then you should use this one

if($_SERVER['REQUEST_URI'] == '/blog' || $_SERVER['REQUEST_URI'] == '/blog/'){

$_SERVER['REQUEST_URI'] = str_replace('/blog', '', $_SERVER['REQUEST_URI']);

} $_SERVER['REQUEST_URI'] = str_replace('/?', '?', $_SERVER['REQUEST_URI']);

7. Set a basic auth method for your blog so it won’t be accessible at https://blog.example.com (especially to google) but only through your rails app /blog URL: https://your-rails-app.com/blog,

I recommend to install wp-basic-authentication plugin https://wordpress.org/plugins/wp-basic-authentication/ which makes it easy to setup a basic auth for your blog

Once the basic authentication is setup, make sure the username and passowrd are set via the reverse proxy (like we have done earlier in step 3)

Go and see your blog live. Ps: if you are using Nginx, read the note below.

Note: if you are using Nginx and you want to use Post name for permalinks, you may find that your posts are not accessible on the blog (getting 404 nginx error) and also on your wordpress dashboard if you try to publish/update a post you will get an error, to solve this refer to my article about wordpress and nginx: https://medium.com/@technoblogueur/wordpress-permalinks-nginx-4180261e8a6

— — — — — — — — — — — — — — — — — — —

Long explanation:

There are already some blog posts out there explaining how, but after following all of them, I was still having issues… if the /blog works, the /blog/wp-admin doesn’t, or /blog/wp-admin redirects in an infinite loop, or sometimes the https://my-rails-app.com/blog URL gets redirected to https://blog.{blog-host}.com and even I ended up with the error “Sorry, you are not allowed to access this page” until… until… I wanted to quit! but… (there is always a “but” to the rescue)

I kept trying to change options until I figured it out, gotchas are everywhere! so yeah, sometimes you just keep trying until you find a solution, and sometimes chance comes into your way and you just find a blog post like this which explain it all…

I hope sincerely this can save someone time, however configurations aren’t the same on every server, so I can’t guarantee it will work 100% for you, it is what worked for me though, so feel free to try it :)

have your blog installed on some server and accessible through some subdomain, for example https://blog.example.com add reverse proxy gem to your application Gemfile

gem 'rack-reverse-proxy', :require => 'rack/reverse_proxy'

3. in your config/application.rb add the following (make sure to replace http://blog.example.com by your blog URL, also note that there is a $1 right after the .com — NO slash between the two)



reverse_proxy_options preserve_host: true

reverse_proxy /^\/blog(\/.*)$/, '

end config.middleware.insert(0, Rack::ReverseProxy) doreverse_proxy_options preserve_host: truereverse_proxy /^\/blog(\/.*)$/, ' http://blog.example.com$1' , :username => 'basic-auth-username', :password => 'basic-auth-password',:timeout => 500end

Note: username and password above are useful for step (7)

GOTCHA: in the configuration above, using the URL with https scheme was preventing me from accessing the blog on my Rails app domain, once I changed https by http, it started to working, you will find some tutorials out there using https, but it didn’t work for me until I turned the url to HTTP! (I’m just talking about the reverse proxy URL here, it’s necessary to have https enabled on your blog server)

4. In your routes.rb add

5. Now log-in to your blog admin area and set WordPress Address (URL) and Site Address (URL) options to your Rails app url + /blog

Note: you can also change wordpress Address and Site Address in your wp-config.php using the following (but keep in mind that this will disabled the inputs you see in the image if you go to the admin area): define('WP_SITEURL', 'https://your-rails-app.com/blog');

define('WP_HOME', 'https://your-rails-app.com/blog'); Another way to change them, is directly in the database, that’s very useful in certain cases, just go to wp_options table, and change the value of siteurl and home fields there.

For me, once I updated these values in the admin area, I was able to see the blog when I visit https://my-rails-app.com/blog, but if I go to https://my-rails-app.com/blog/wp-admin it fails, and kept failing until I found the magic piece of code that saved me, here is it in step 6:

6. Add the following to the top of wp-config.php

define('FORCE_SSL_ADMIN', true); if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')

$_SERVER['HTTPS']='on'; if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {

$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];

} // NOTE: ===> if your Wordpress is installed physically under a /blog folder in your server, then the line below should be omitted. $_SERVER['REQUEST_URI'] = '/blog'.$_SERVER['REQUEST_URI'];

That piece of code solved everything for me, all I had to do is to add it to my wp-config.php at the VERY TOP, just right after the <?php (the emplacement is important here, especially the FORCE_SSL_ADMIN line)

The last 3 lines I got them copied from https://www.netlingshq.com/blog/wordpress-with-ruby-on-rails/ (many thanks to the author by the way)

but the very first line was the thing that saved my day, without it I wasn’t able to access the wp-admin area ever (thanks to answer from https://wordpress.stackexchange.com/a/307509/60613) so be sure to add define(‘FORCE_SSL_ADMIN’, true); at the very top of wp-config.php (especially if you see the error “you are not allowed to access this page”)

I can also see the benefit of the last line

$_SERVER['REQUEST_URI'] = '/blog'.$_SERVER['REQUEST_URI'];

Please note that this line is only necessary if your blog was installed at the root of your server, if the blog was installed physically under a /blog folder (which means it’s accessible via https://blog.host-server.com/blog) then you should omit that line, no need to prepend the ‘/blog’ to your REQUEST_URI.

I think this is very important, because at some point, I was noticing that even by changing Wordpress Address (URL) and Site Address (URL) options, wordpress was using $_SERVER[‘REQUEST_URI’] internally for some redirection, for example, I was accessing the theme customizer from the sidebar menu and when trying to close it (X) I was redirected to https://my-rails-app.com/wp-admin instead of https://my-rails-app.com/blog/wp-admin (that’s caused a 404 error on my Rails app), same thing for the permalinks page where the form has a hidden field as follows:

<input type="hidden" name="_wp_http_referer" value="wp-admin/options-permalink.php">

That’s caused the form to redirect back to /wp-admin instead of /blog/wp-admin, fortunately by prepending ‘/blog’ to $_SERVER[‘REQUEST_URI’] the correct prefix was added:

<input type="hidden" name="_wp_http_referer" value="/blog/wp-admin/options-permalink.php">

6.1) If you are going to use a Static Page as your home page instead of the default one (which is “Your latest posts”) you may encounter the problem “The page isn’t redirecting properly”

The “hack” I did to solve this is replacing the line

$_SERVER['REQUEST_URI'] = '/blog'.$_SERVER['REQUEST_URI'];

by

// Prepend '/blog' to REQUEST_URI only if it's different than '/' to prevent "The page isn’t redirecting properly" issue: if($_SERVER['REQUEST_URI'] !== '/'){



$_SERVER['REQUEST_URI'] = '/blog'.$_SERVER['REQUEST_URI'];

} // some theme builders add query string to edit url, which causes the home page (/) to have an incorrect pattern (/?) that leads to page redirection issue (when you click edit with builder)! the following line will fix it: $_SERVER['REQUEST_URI'] = str_replace('/?', '?', $_SERVER['REQUEST_URI']);

If your wordpress is physically installed on a “/blog” folder instead of the root of server then you should use this one

if($_SERVER['REQUEST_URI'] == '/blog' || $_SERVER['REQUEST_URI'] == '/blog/'){

$_SERVER['REQUEST_URI'] = str_replace('/blog', '', $_SERVER['REQUEST_URI']);

} $_SERVER['REQUEST_URI'] = str_replace('/?', '?', $_SERVER['REQUEST_URI']);

7. Set a basic auth method for your blog so it won’t be accessible at https://blog.example.com (especially to google) but only through your rails app /blog URL: https://your-rails-app.com/blog,

I recommend to install wp-basic-authentication plugin https://wordpress.org/plugins/wp-basic-authentication/ which makes it easy to setup a basic auth for your blog

Once the basic authentication is setup, make sure the username and passowrd are set via the reverse proxy (like we have done in step 3)

Note: if you are using Nginx and you want to use Post name for permalinks, you may find that your posts are not accessible on the blog (getting 404 nginx error) and also on your wordpress dashboard if you try to publish/update a post you will get an error, to solve this refer to my article about wordpress and nginx: https://medium.com/@technoblogueur/wordpress-permalinks-nginx-4180261e8a6

To be honest, after dealing with all the above gotchas, I started to feel how messy Wordpress can be, and if it’s worth to use it or not…

I hop this post was helpful.