Running a ZendFramework project inside a subdirect

2019-08-08 17:51发布

问题:

I have a ZendFramework project that I would like to run inside a subdirectory.

Currently my document root is at /var/www and the project is at /var/www/project with the various folders (controllers, public, etc.) all within the project directory.

I am trying to make it so that all requests from http://webserver/project/* are being passed to the /var/www/project/public/index.php file. However, trying to use the following code in /var/www/project/.htaccess to pass all requests does not work:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /project/public/index.php [NC,L]

There is also an .htaccess file in /var/www/project/public that contains exactly the same code, but when loading the /project URL I am just presented with a directory index of the project contents.

回答1:

I'd store the project completely separate to the document root, for example /home/user/projects/project.

Then, simply symlink /var/www/project to /home/user/projects/project/public.

Provided you use the BaseUrl view helper for any static asset links (JavaScript, images, CSS, etc), it should work just fine.

Edit: Another suggestion is to separate your public directory from the rest of the application.

For example, say your project is in /home/user/projects/project, move the contents of your public directory to /var/www/project and edit index.php with the following

defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', '/home/user/projects/project/application');


回答2:

I know this is an old question and the asker has long since figured out his problem but I just ran into it. This is the solution I found that worked the simplest and hopefully anybody else that arrives here will see this.

In your VirtualHost file simply add an Alias right about your tag to create a VirtualHost file like this

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin admin@example.com
    DocumentRoot /home/user/example.com/public_html/
    ErrorLog /home/user/example.com/logs/error.log
    CustomLog /home/user/example.com/logs/access.log combined

    Alias /zend/albums /home/user/example.com/zf2-tutorial/public 

    <Directory /home/user/example.com/public_html>
        AllowOverride All
        Header set Access-Control-Allow-Origin: "http://localhost"
    </Directory>
 </VirtualHost>

Edit: Just figured out you will have to edit your .htaccess (in the public directory) to include this line (going with the above example):

RewriteBase /zend/albums

Here's a pretty good answer by Phil that's similar to mine but he removes his .htaccess files completely and instead puts the rules inside the VirtualHosts file. https://stackoverflow.com/a/7563414/1031714



回答3:

Remove the first RewriteRule: it is saying for all addresses rewrite to nothing (-) and stop ([L] flag).

Edit: This should fits your needs

RewriteCond %{REQUEST_FILENAME} !project/public/.*
RewriteRule ^project/(.*)$ project/public/$1 [NC,L]       

RewriteCond %{REQUEST_FILENAME} !-s      
RewriteCond %{REQUEST_FILENAME} !-l      
RewriteCond %{REQUEST_FILENAME} !-d      
RewriteRule ^.*$ /project/public/index.php [NC,L] 

This rewrites any request to project/ not containing public/ to project/public/. The next one redirects any call to non existing file or dir to index.php in the same directory.