.htaccess, document root and Zend Framework

It’s funny but I couldn’t find anyone having this issue. It certainly isn’t limited to Zend Framework, it affects everyone who doesn’t have access to apache configuration and wants to have document root in a deeper directory. Maybe I just can’t google very well.

Imagine a shared host. You can’t choose your document root, it’s firmly set to one particular directory. Now you want to install Zend Framework. Imagine you’d like to follow the standard directory structure (that means you more or less have these directories in your project: application (the application), library (zend and other libraries) and public (stuff accessible from the outside, images, css etc.)). Normally, you’d want to point your document root to the public directory, but when you can’t do that you can use .htaccess in the project directory to redirect everything to public.

RewriteEngine On
RewriteRule !\.(js|gif|jpg|png|css|txt)$ public/index.php [L]
RewriteRule ^(.*)$ public/$1 [L]

This was the first thing I wrote, and it works on certain servers. Other servers will kindly remind you that “Request exceeded the limit of 10 internal redirects due to probable configuration error.” Which kind of makes sense when you think about it. Makes me wonder how comes the above code actually worked on two different configurations.

Now here’s the code that works (and in my humble opinion it also should work, as opposed to the one above):

RewriteEngine On
RewriteRule !\.(js|gif|jpg|png|css|txt)$ public/index.php [L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/$1 [L]

And finally a disclaimer: I am not sure about the security implications. If someone knows more about possible security issues with this, please do leave a comment.

6 thoughts on “.htaccess, document root and Zend Framework

  1. Why on earth would you want to install Zend Framework?!?

    Anyway, I suspect your first try at rewriting works with Apache 2.2 but not Apache 2.0, however, I’m too lazy to test it.

  2. Hi, Your solution is really effective but i test it in different aspect and failed in some point. for example BaseUrl access with this solution isn’t possible and you couldn’t access to image folder in include in some of servers.

    Thanks.

  3. Ah, thanks for that. I was having a redirect problem w/ ZF as well. This wasn’t the exact problem, but it remind me to look at my .htaccess, which I’d forgotten to update when I moved my project files around :)

Comments are closed.