Zend Framework and web hosting services

Posted on Tuesday, November 24th, 2009 under , ,

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

Related posts

7 Responses to “Zend Framework and web hosting services”

Trackbacks (1)

Comments (6)

  1. • dt •

    I don’t get it. Why don’t you put your library directly in your home directory, above public_html?

    /home/{your-username}/library/

  2. Tudor

    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.

  3. • dt •

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

  4. Tudor

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

  5. Radu

    On the other hand those .htaccess files do create a little overhead for the server compared to not using them… But you can create symlinks by placing a cronjob in Cpanel. ;)

  6. Tudor

    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

Leave a Reply