Friday, January 28, 2011

[Apache] How to serve dynamic pages outside the document root?

Hi,

I'm trying to setup a simple web app and I'm having some difficulties.

Here's how I have it setup :

/srv/www/application <-- python code
/srv/www/public_html <-- document root

I want Apache to serve everything in public_html if he can find it, else send the request to my application.

a snippet of my virtual host file :

    DocumentRoot /srv/www/public_html

    <Directory /srv/www/public_html>
            Options -Indexes
            RewriteEngine On
            RewriteBase /
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ ../application/code.py/$1 [L]
    </Directory>

I'm really not sure about the ../application, (because I think it rewrites to a url, not a directory), and I suspect this is why it's not working, but I can't find a way to make it work.

Update

I've applied Cakemox suggestion, but there was still some problems. Here's what I did :

WSGIScriptAlias / /srv/www/application/code.py

This route everything to my application. Strangely, it worked for a moment, i.e file found inside public_html was served and the rest was routed to my application, but I guess the browser's cache was playing tricks on me.

I also tried :

WSGIScriptAlias /application /srv/www/application/code.py

<Directory /srv/www/public_html>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ application/$1 [L]
</Directory>

This worked most of the time, except that the default url (mydomain.com) was listing my public_html directory instead of running the app. I guess it's because the requested filename is /srv/www/public_html, which is a directory so the condition is false. It's weird because I've numerous app running perfectly fine with those conditions...

Update 2 After almost 2 days of head-smashing-on-keyboard (I can touch my brain now), I think I finally got it. The "numerous app" I was referring to in the last paragraph were mostly PHP apps where the application index.php is in the public_html folder. So browsing mydomain.com, Apache first search for the default index (DirectoryIndex) before applying any rewriting rules. Since I do not have any index.php, index.htm or index.html in my public_html folder, it just listed the directory. So to fix this, I can either add DirectoryIndex /application or RewriteRule ^$ /application before the conditions. Here's the final snippet :

<Directory /srv/www/public_html>
        DirectoryIndex /application
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ application/$1 [L]
</Directory>

Thanks.

  • Alias comes to mind:

    Alias /application "/srv/www/application"
    
    Subb : Yes, but since my public_html folder will be (somewhat) public, someone could create an "application" directory and break the whole thing, because Apache will serve the directory instead of the app.
    Cakemox : The alias directive has priority over whatever is in the document root. Even if someone adds an application directory, the alias takes precedence.
    Subb : You're right. I guess I was just trying too much with mod_rewrite. Thanks!
    Subb : I guess it didn't work after all. See update.
    From Cakemox

0 comments:

Post a Comment