16

I'm new to Zend Framework. I would like to know how to implement zend framework on a shared hosting. Because of the zend framework folder structure all view files are put into the "public" folder.

Suppose

"/" is the main root folder for me and public is like "/public"

so that the url becomes "http://site/public/. .. .bla bla..."

is this correct?

or is there any other method?

i dont have any permission to create a virtual host.

so what to do?

I hope that you understood my question. If not, please ask me.

Thank you!

markus
  • 40,136
  • 23
  • 97
  • 142
coderex
  • 27,225
  • 45
  • 116
  • 170

12 Answers12

12

i think the best way is to remove the .htaccess from the public directory (Zend Framework Directory structure) , and put it with the following content into your "root" directory :

 
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]

opHASnoNAME
  • 20,224
  • 26
  • 98
  • 143
  • hi - just wondering can you spot anything wrong with the details i posted at the bottom of this thread? – emeraldjava Sep 03 '09 at 12:30
  • Possible n00b question: What effect will this have on ZF routing? That is, won't the "/public" prefix be part of the url that ZF sees (I guess via the $request object)? As a result, do all ZF Routes now need to be "/public" aware? That would be pretty undesirable, wouldn't it? – David Weinraub Dec 10 '09 at 07:10
  • where to put the setting of environment? – JellyBelly Apr 19 '12 at 09:04
  • 2
    this not work with modules! If i go to www.domain.com/admin where admin is module, i'm redirected to www.domain.com/public/admin. how fix this please? – JellyBelly May 09 '12 at 13:54
  • This worked terribly. I didn't have time to spot the redirects that were happening, but there were may conditions that were not met during requests. With Zend Modules, this causes infinite loops at certain points. This could work with more work. – axiom82 Mar 25 '14 at 15:03
11

Include this .htacces file under your base path (that is /../public):

RewriteEngine On

# Exclude some directories from URI rewriting
#RewriteRule ^(dir1|dir2|dir3) - [L]

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]

And leave the .htaccess that was under the publc directory where it was.

So you will have 2 .htaccess files, one under the public directory (the ordinary one from Zend Framework documentation) and second one under your base path (the one I posted above).

Richard Knop
  • 81,041
  • 149
  • 392
  • 552
  • hi - just wondering can you spot anything wrong with the details i posted at the bottom of this thread? – emeraldjava Sep 03 '09 at 12:31
  • I tried this, but it does not find other directories, which link to application folder outside the public folder. Other simple pages work – mrN Oct 11 '10 at 10:52
2

You should place your project's Public folder to www folder in the host, so your address will become yourdomain.com/. Your source code folder such as library, applications... should be place at same level with www folder, not within it. This method will promote the security of your site

Thang Nguyen
  • 1,161
  • 4
  • 16
  • 30
1

Indeed it's not the best idead to run Zend Framework applications on a shared hosting. I would really recommend getting a virtual private hosting (VPS). There are very good and inexpensive hostings out there with Zend Framework and other frameworks already installed and regularly updated. I'm on servergrove and it has been great so far!

But this doesn't mean that you can't make it work on a shared hosting. You just have to rather work with .htacess. Put the content of the public folder into your webroot and adjust your paths in the bootstrap.php, make sure all other folders cannot be accesses directly and use the usual ZF approach of routing everything through your index.php.

markus
  • 40,136
  • 23
  • 97
  • 142
1

Also Rob Allen wrote a good solution

I'm using Zend Framework 2 and it worked for me. Alghough I haven't done part "Referencing public facing files" from above link.

I just changed code in my layout.phtml from:

$basePath = $this->basePath();

to:

$basePath = $this->basePath();
if (defined('RUNNING_FROM_ROOT')) {
    $basePath .= 'public/';
}
Pavel Kostenko
  • 1,859
  • 1
  • 18
  • 18
  • Хорошее решение. А вы не знаете как можно установить этот `basePath` так, чтобы на всё приложение он был одинаковый? При таком решении придется в каждом виде, где есть вызов `$this->basePath()`, прописывать этот код. А хотелось бы в одном месте где-то задать его и везде бы он был один и тот же. – Green Dec 19 '12 at 15:31
  • This would require all phtml files to be edited :( any shorter method for a faster workaround? – LoneWOLFs Aug 07 '13 at 11:26
1

The answer from ArneRie did not work for me and I finally ended up using Lorenzo Albertons solution. Hope this is helping others to get a faster solution.

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]
lony
  • 6,733
  • 11
  • 60
  • 92
1

Actually I've searched for it and found a lot of answers (most of them here on SO), but none of them worked for me. So I finally found a solution - .htaccess on root dir:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php

This one from http://framework.zend.com/wiki/display/ZFDEV/Configuring+Your+URL+Rewriter and index.php on root dir:

<?php 
define('RUNNING_FROM_ROOT', true);
include 'public/index.php';
?>

This one from http://akrabat.com/zend-framework/zend-framework-on-a-shared-host/ And it works for me even if there are other project dir in root folder - like forum or so. Thought it might be useful to someone :)

Karmalakas
  • 1,143
  • 2
  • 19
  • 39
0

I've been in the same situation and have put the zend libraries under a folder under public like 'src' - and use an .htaccess Deny from All. You'll have to juggle a couple paths, but it works fine.

Justin
  • 5,029
  • 1
  • 21
  • 21
0

. . . How Magento uses Zend framework??? I'm using XAMPP and I did install Magento locally without modify php.ini, http.conf nor virtual host.

0

I encountered this problem recently and through this thread i was able to find a solution. I am using zf2. So all i had to do was have the zend related files in a directory we can name say src. This directory will reside in the public or root directory (such as /www)

  1. Create a .htaccess file in the 'src' dir and add the Deny from All directive.

  2. In the index file, change the working directory to the src folder by doing the following:

    define('SRC_ROOT', 'src');
    chdir(SRC_ROOT);
    

...and you are good to go!

mico
  • 12,730
  • 12
  • 59
  • 99
0

Setting up the virtual host is usually done within httpd.conf or extra/httpd-vhosts.conf. If you are using httpd-vhosts.conf, ensure that this file is included by your main httpd.conf file. Some Linux distributions (ex: Ubuntu) package Apache so that configuration files are stored in /etc/apache2 and create one file per virtual host inside folder /etc/apache2/sites-enabled. In this case, you would place the virtual host block below into the file /etc/apache2/sites-enabled/zf2-tutorial.

Ensure that NameVirtualHost is defined and set to “*:80” or similar, and then define a virtual host along these lines:

Setting up the virtual host is usually done within httpd.conf or extra/httpd-vhosts.conf. If you are using httpd-vhosts.conf, ensure that this file is included by your main httpd.conf file. Some Linux distributions (ex: Ubuntu) package Apache so that configuration files are stored in /etc/apache2 and create one file per virtual host inside folder /etc/apache2/sites-enabled. In this case, you would place the virtual host block below into the file /etc/apache2/sites-enabled/zf2-tutorial.

Ensure that NameVirtualHost is defined and set to “*:80” or similar, and then define a virtual host along these lines:

 <VirtualHost *:80>
     ServerName zf2-tutorial.localhost
     DocumentRoot /path/to/zf2-tutorial/public
     SetEnv APPLICATION_ENV "development"
     <Directory /path/to/zf2-tutorial/public>
         DirectoryIndex index.php
         AllowOverride All
         Order allow,deny
         Allow from all
     </Directory>
 </VirtualHost>

Make sure that you update your /etc/hosts or c:\windows\system32\drivers\etc\hosts file so that zf2-tutorial.localhost is mapped to 127.0.0.1.

Make sure that you update your /etc/hosts or c:\windows\system32\drivers\etc\hosts file so that zf2-tutorial.localhost is mapped to 127.0.0.1.

-1

I suggest the simpliest: Develop your apps in a way where index.php is in your root (even better - always define PUBLIC_PATH - dirname(__FILE__); in your index.php and make links relative to this constant). Along with the application and library folders. You can easily make them unaccessible from outside using deny from all in .htaccess.

The PUBLIC_PATH constant also helps when your public is not public. (public html or /public html/www (in my case))

Tomáš Fejfar
  • 11,129
  • 8
  • 54
  • 82