Let’s face it. We can’t all buy or rent our own servers. It’s not cost effective. So we turn to hosting companies – always the popular choice. But when you’re using an shared hosting service, you can’t make changes in the server’s configuration file. You usually get a writeable directory in a location like
/home/{your-username}/public_html/
…and a Cpanel account to manage your share of the server, emails, databases and so on. And…that’s about it. You can’t point your webroot to the public directory as taught in Zend Framework’s manual, all your library files will be exposed to the public and so on. What to do then? The answer is simple: .htaccess and mod_rewrite. Usually, hosting companies install mod_rewrite and set AllowOverride to true, so this solution will work in the vast majority of cases.
Upload via FTP or SSH your content into your public_html, wwwroot or www directory (the webroot directory, whatever its name may be). Then add 4 htaccess files, one directly in the web root, one in the public directory and one in all other folders. It should look something like this (without the numbers, of course).
public_html/
/public
.htaccess (2)
/application
.htaccess (3)
/library
.htaccess (3)
.htaccess (1)
Now, the first htaccess file – #1 – should contain the following:
RewriteEngine On RewriteRule ^(.*)$ public/$1 [L]
…this will redirect the traffic from this folder to the public/ sub-folder. The second .htaccess – the one in the public folder – should be the .htaccess file shipped with Zend Framework. It usually looks like this:
SetEnv APPLICATION_ENV production
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
And the .htaccess files located in all other directories – #3 – should simply forbid access to their respective folders, like such:
deny from all
Worked for me
I don’t get it. Why don’t you put your library directly in your home directory, above public_html?
/home/{your-username}/library/
Because if you rename the public_html folder to public, to comply with Zend Framework’s directory structure, the server won’t work.
You can hack the Framework and force it to use a public_html folder instead of a public folder (have a look into Zend/Controller/Front.php and Zend/Controller/Request/Http.php) but that’s generally a bad idea. A lot of people expect the /public/ directory to be there and you might have “surprises” with 3rd party components that also expect a /public/ folder. I’ve seen a component that was looking for a path like /public/images/avatars/ to save the user’s avatar. These kind of components will also need to be re-written.
Another idea is to delete the /public_html/ folder as it is and create a symlink in its place, from /public_html/ (where the server want the document root) to /public/ (where the framework wants the document root). But since most hosting company don’t provide a shell account, I don’t know if you can do this with FTP alone. Any help will be appreciated
The solution with .htaccess files is the simplest I’ve seen so far and won’t mess around with other plugins’ functionalities.
Ok, I see. You can’t remove public_html because this will break the apache configuration but you can create a symlink “public” to point to public_html Symlink can be created without shell, from a cgi script. Or, a quickfix from FTP: just rename www into public (www is a precreated symlink to public_html). I test it and it works. Anyway, if you cannot create it, just ask your provider
)
I prefer the easy way: adding a few .htaccess files here and there, as it works right out the box, no need to rename anything or bother the hosting company with requests.
I usually bother my provider with stuff like “can you open port xyz in the firewall”, or “quick, give me last night’s backup”. I think “he” hates me
But, of course, you already know that
On the other hand those
.htaccessfiles do create a little overhead for the server compared to not using them… But you can create symlinks by placing a cronjob in Cpanel.The overhead is minimal…the only “heavily used” .htaccess will be the one placed directly in the “public_html” folder which redirects all the traffic to the /public/ sub-folder
[...] Continued here: Zend Framework and web hosting services :: Zend Framework on … [...]
Hi i’ve just tried this method and i got error 500 The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@xxxx.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Any Idea? thanks
I have used the .htaccess technique but a 500 error raised telling me that there is a miss configuration in the .htaccess file. can anyone tell me the reason
hi,
i have used same steps but i m getting ,
Fatal error: Uncaught exception ‘Zend_Db_Adapter_Exception’ with message ‘The PDO extension is required for this adapter but the extension is not loaded’ in /home/mobile/public_html/zendy/library/Zend/Db/Adapter/Pdo/Abstract.php:342 Stack trace: #0 /home/mobile/public_html/zendy/library/Zend/Db/Adapter/Abstract.php(247): Zend_Db_Adapter_Pdo_Abstract->setFetchMode(2) #1 /home/mobile/public_html/zendy/library/Zend/Db.php(257): Zend_Db_Adapter_Abstract->__construct(Array) #2 /home/mobile/public_html/zendy/library/Zend/Application/Resource/Db.php(142): Zend_Db::factory(‘PDO_MYSQL’, Array) #3 /home/mobile/public_html/zendy/library/Zend/Application/Resource/Db.php(154): Zend_Application_Resource_Db->getDbAdapter() #4 /home/mobile/public_html/zendy/library/Zend/Application/Bootstrap/BootstrapAbstract.php(681): Zend_Application_Resource_Db->init() #5 /home/mobile/public_html/zendy/library/Zend/Application/Bootstrap/BootstrapAbstract.php(624): Zend_Application_Bootstrap_BootstrapAbstract->_executeResource(‘db’) #6 /home/mobile/pub in /home/mobile/public_html/zendy/library/Zend/Db/Adapter/Pdo/Abstract.php on line 342
please can you help me ….
Install PDO http://www.php.net/manual/en/pdo.installation.php