.htaccess, document root and Zend Framework

2008-10-29

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]</code></p> <p>This was the first thing I wrote, and it works on <strong>certain</strong> servers. Other servers will kindly remind you that <em>"Request exceeded the limit of 10 internal redirects due to probable configuration error."</em> Which kind of makes sense when you think about it. Makes me wonder how comes the above code actually worked on two different configurations.</p> <p>Now here's the code that works (and in my humble opinion it also <strong>should</strong> work, as opposed to the one above):</p> <p><code lang="apache">RewriteEngine On<br /> 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.

← How I Met Your Mother How to get an extra hour a day →

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

2008-11-10

:-x :-x :-x :-x

Michael Goetze 2008-11-13

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.

Amin 2008-12-22

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.

Brian 2009-01-09

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 :)

Richard Knop 2009-03-14

Here is imho a better solution for this issue:

http://www.alberton.info/zend_framework_mod_rewrite_shared_hosting.html

Regards, Richard

tasuki 2009-03-14

Thanks, Richard – that solution is definitely better as it doesn’t rely on a list of file extensions.

Add your commentHow does this work?