February 08, 2010 by Dave

Have you ever wanted to run a PHP application inside of a Rails application? Here's a scenario: your website is written in ruby on rails. You want to run a php application, like Magento inside it, under a directory called shop. Since all web-accessible files in Rails are located in the public directory, your shop directory will be located in public/shop. However, whenever you try to access this directory by going to http://www.yourwebsite.com/shop, You get a 500 error from your Rails application saying: Application error Rails application failed to start properly, or something along those lines. 

Uh Oh.

This is because rails thinks that this subdirectory is a controller or an action within your rails application that isn't acting properly, when in reality, it's a real subdirectory with PHP files inside! We need to convince it otherwise. The easiest way to do this is by creating an .htaccess file inside the shop directory. Then paste this code into the .htaccess file:

RewriteEngine On
RewriteRule ^([^\.]+[^/])$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_URI} !^/-
RewriteCond %{DOCUMENT_ROOT}/-%2 -d
RewriteRule ^(.*)$ -%2/$1 [QSA,L]

That's it! Try it again, and your PHP code should run without a problem! 




Mar 15 2010 at 08:47 PM

hi, this doesn't seem to work for me


Mar 15 2010 at 09:49 PM

it does work, thank you! however, although /shop/index.php seems to work, just /shop/ does not.


Aug 20 2010 at 03:10 AM

Have you tried adding DirectoryIndex index.php to the .htaccess file?


May 19 2011 at 01:11 PM

is there any need to restart apache server after doing this


Jul 12 2011 at 08:06 PM

@Kausik - Nope, changes to .htaccess should be immediate as long as Apache's AllowOverride is working properly.

Cumpanasu Florin

Nov 06 2011 at 12:42 AM

Not working for me. Instead of running, he is downloading the file.php


Apr 22 2013 at 01:57 PM

What if for instance you have a php application running in the root of your domain, say wordpress but you have a ruby application running in a subdirectory of the domain and you wish to run a php include from within a .rhtml file, lets say for instance a header.php file located in wordpress that contains a nav menu and you wish that to be pulled into a header.rhtml file of your ruby installation so that your header menu is reflected across both installations.

How would you be able to go about that? It seems to be in reverse of the above.


Add A Comment

Completely Private, used for Gravatar

example: https://www.whatever.com