在子目录中运行 ZendFramework 项目

发布于 2024-10-31 07:08:39 字数 837 浏览 2 评论 0原文

我有一个 ZendFramework 项目,我想在子目录中运行。

目前,我的文档根目录位于 /var/www ,项目位于 /var/www/project ,其中包含各种文件夹(controllers、< em>public 等)都在 project 目录中。

我正在尝试将来自 http://webserver/project/* 的所有请求传递给 < /var/www/project/public/index.php 文件。但是,尝试在/var/www/project/.htaccess中使用以下代码来传递所有请求是行不通的:

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]

/var/www/project/.htaccess中还有一个.htaccess文件>/var/www/project/public 包含完全相同的代码,但在加载 /project URL 时,我只会看到项目内容的目录索引。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乱世争霸 2024-11-07 07:08:39

我会将项目完全独立于文档根目录存储,例如 /home/user/projects/project

然后,只需将 /var/www/project 符号链接到 /home/user/projects/project/public

如果您对任何静态资源链接(JavaScript、图像、CSS 等)使用 BaseUrl 视图助手,它应该可以正常工作。

编辑:另一个建议是将公共目录与应用程序的其余部分分开。

例如,假设您的项目位于 /home/user/projects/project 中,请将 public 目录的内容移动到 /var/www/project code> 并使用以下内容编辑 index.php

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

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');
傲娇萝莉攻 2024-11-07 07:08:39

我知道这是一个老问题,提问者早已找到了他的问题,但我刚刚遇到了它。这是我发现最简单的解决方案,希望到达这里的其他人都能看到这一点。

在您的 VirtualHost 文件中,只需在您的标签上添加一个别名即可创建一个像这样的 VirtualHost 文件

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin [email protected]
    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>

编辑:刚刚发现您必须编辑您的 .htaccess (在 public 目录中)以包含此行(继续上面的例子):

RewriteBase /zend/albums

这是 Phil 的一个非常好的答案,与我的类似,但他完全删除了他的 .htaccess 文件,而是将规则放入 VirtualHosts 文件中。 https://stackoverflow.com/a/7563414/1031714

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 [email protected]
    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

汹涌人海 2024-11-07 07:08:39

删除第一个 RewriteRule:它表示所有地址都重写为空(-)并停止([L] 标志)。

编辑:这应该适合您的需求

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] 

这会将任何对project/不包含public/的请求重写为project/public/。
下一个将对不存在的文件或目录的任何调用重定向到同一目录中的index.php。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文