A quick tip on how to deal with Zend Framework directory structure and the public document root directory in most shared hosting accounts, using mod_rewrite and an .htaccess file.
Here's a short tip for those who need to publish a Zend Framework project on a shared hosting. The typical project structure for a Zend Framework project is something like this:
projectname/
application/
controllers/
views/
scripts/
library/
public/
tests/The public directory contains all the files that should be directly accessible via the web server, so you must set your web server's document root to this directory.
The problem is, in most shared hosting setups you can't change the document root, and the root directory of your account (i.e. the uppermost level you see when you connect via FTP) is the public document root.
To solve this problem without changing the above directory structure, you can remove the .htaccess file from the public directory, and place this one in the root directory instead:
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]This is an effective way to "move" the document root up one level to the public directory.
Lorenzo
14 responses to "Zend Framework, mod_rewrite and public dir in shared hosting"
Thanks so much, helped a lot!!
Thank you for the tip, very helpful.
I can keep on developing Zend Framework projects on low-budget shared-hostings. Thanks so much, really interesting trick !!!
I am create .htaccess in project document root and delete .htaccess in public folder but sorry to say that its not working . you need to mention the location of index.php is it in public folder or document root .
You don't have to touch anything else. The index.php file remains in the /public folder.
Use your Technique i Get This result. Can you attached it. That will be helpful
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster. Error 404
Hello,
I tried your solution on a wamp installation but i've got a HTTP 404.
Any idea
Hi,
I tried this, but it only kinda half-worked.
i'm using http://www.abc.com/TestProject
so I placed the .htaccess into the "TestProject" folder on my web host. The .htaccess didnt work for my host, so I modified it as such:
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /home/abc/public_html/TestProject/public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /home/abc/public_html/TestProject/public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /home/abc/public_html/TestProject/public/index.php [NC,L]
Now if I type http://www.abc.com/TestProject/index.php it works. So does http://www.abc.com/TestProject/public/
However I get a Zend File not found error when I try http://www.abc.com/TestProject. Any help will be greatly appreciated.
> RewriteCond %{REQUEST_URI} =\"\" > RewriteRule ^.*$ /public/index.php [NC,L]
Why not:
RewriteRule ^$ public/index.php [L]
for zend within a folder you should use virtual hosts here's a link which provides a nice tutorial. http://johnbokma.com/windows/apache-virtual-hosts-xp.html
Great article and great setup! Thanks for the post. @Marius, some shared hosting environments do not allow for virtual hosts but instead only subdomains.
Many thanks! One question: If the project is not in the webroot zend, but in a subfolder?
Example webroot: - Files - Project zend --- .htaccess --- application --- library --- public --- tests
I tried to put the htaccess in the subfolder you reported zend project, but by error 404!
How can I fix? Thanks
@JellyBelly, I don't know, try moving the .htaccess file one level up (to the root), and replacing public/ with zendproject/public/ in the .htaccess rules.
I finally found what I wanted. Excellent guide.