Apache DirectoryIndex directive not working correctly with Ruby on Rails

842 views Asked by At

I have a site where Ruby on Rails is running at the root directory via Phusion Passenger. Some Apache Alias directives serve static HTML directories.

If you request an exact URL of a file from one of the static directories, Apache returns it as expected. However, there are a few cases where I'm not getting the expected behavior. Consider the alias Alias /functional /path/to/functional, where the functional directory contains index.php and hello.jpg.

  1. A request for https://example.com/functional/hello.jpg returns the JPEG file as expected.
  2. A request for https://example.com/functional/index.php runs the PHP script and prints the resulting HTML document as expected.
  3. A request for https://example.com/functional/ results in a 404 in the Rails stack.
  4. A request for https://example.com/functional (without the trailing slash) also results in a 404 in the Rails stack. Ideally this URL should return a 301 redirecting the user to http://example.com/functional/

If I add an index.html file into the functional directory (in addition to the index.php that is already there), paths #3 and #4 return that file as expected.

Here's the relevant parts from my Apache configuration file for Passenger:

LoadModule passenger_module /path/to/mod_passenger.so
LoadModule ssl_module modules/mod_ssl.so

<IfModule mod_passenger.c>
    # ... passenger setup stuff omitted ...

    Listen 443
    <VirtualHost *:443>
        ServerName ...
        DocumentRoot /path/to/rails/public
        PassengerRuby /path/to/ruby
        <Directory /path/to/rails/public>
            Allow from all
            Options -MultiViews
        </Directory>

        #  ... ssl setup omitted ...

    </VirtualHost>
</IfModule>

And for the static directories:

Alias /functional /path/to/functional/
Alias /phpMyAdmin /path/to/phpMyAdmin/
# ... other aliases omitted ...

<Directory /path/to/functional>
    DirectoryIndex index.php
    AllowOverride all
</Directory>

I've tried most of the answers in index.php not loading by default to make index.php load, to no prevail. I shouldn't even need to specify DirectoryIndex index.php since that line is already in conf.d/php.conf.

Any ideas on how to solve this issue where Rails is giving 404s on URLs which are supposed to be going elsewhere in the Apache stack?

2

There are 2 answers

0
Sjoerd van Groning On

Have you tried replacing

<Directory /path/to/functional>

With

<Directory /functional>
0
Amir Masud Zare Bidaki On

I had the same problem. The working solution was just a RedirectMatch statement in the virtualhost serving passenger as follows:

RedirectMatch  ^/path/to/functional(/)?$  /path/to/functional/index.php