1

大家好,我在使用 htaccess 的 ZF2 中遇到问题。

我创建了虚拟主机,一切正常。当我打电话时

my-vhost.localhost一切正常,但是当我添加一些 uri 段,如 /index.php 或 /1234 时,我得到 404。

我在公共目录中编辑 .htaccess 并放入

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [NC,L]

再一次,我有 404。知道我怎样才能减慢这个速度吗?

我的完整 .htaccess 是:

RewriteEngine On

# The following rule tells Apache that if the requested filename
# exists, simply serve it.

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

虚拟主机

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

谁认为问题是路由检查我的路由模块。

<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'Album\Controller\Album' => 'Album\Controller\AlbumController',
        ),
    ),

    // The following section is new and should be added to your file
    'router' => array(
        'routes' => array(
            'album' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/album[/][:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Album\Controller\Album',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'album' => __DIR__ . '/../view',
        ),
    ),
);
4

2 回答 2

2

附带的 .htaccess 文件内容如下;

RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

它与您的副本略有不同,我在一个工作示例中尝试了您的 .htaccess 文件,它看起来很好。

404 您获得的是通用的“服务器外观”404 还是 ZF2 主题页面?

可能是一个愚蠢的建议,但您是否尝试过http://my-vhost.localhost/publichttp://my-vhost.localhost/public/album ???

据我所知,您不需要直接调用 .php 文件

于 2013-08-20T18:49:45.950 回答
2

我不确定现在回答这个问题为时已晚,因为 Finbarr 建议尝试使用 http://my-vhost.localhost/publichttp://my-vhost.localhost/public/album

每次我输入 www.zf2-tutorial.loc 时,我也遇到了同样的问题,它总是被重定向到 xampp 主页。

最后,我阅读了我更改为 Finbarr 的评论

http://www.zf2-tutorial.loc/zf2-tutorial/public/

之后网页:Welcome to Zend Framework 2 被加载。

#the virtual host:

名称虚拟主机 *:80

<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
</VirtualHost>

<VirtualHost *:80>
<DocumentRoot "C:/xampp/htdocs/zf2-tutorial/public"
ServerName  www.zf2-tutorial.loc zf2-tutorial.loc
ServerAlias zf2-tutorial.loc
<Directory "C:/xampp/htdocs/zf2-tutorial/public">
    DirectoryIndex index.php
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>
</VirtualHost>

在主机中,我做了几个组合,试图找出哪个是正确的。

127.0.0.1   localhost
127.0.0.1   www.zf2-tutorial.loc  zf2-tutorial.loc
127.0.0.1   www.zf2-tutorial.loc  zf2-tutorial.loc localhost
127.0.0.1   www.zf2-tutorial.loc  localhost
127.0.0.1   www.zf2-tutorial.loc
127.0.0.1   zf2-tutorial.loc localhost
127.0.0.1   zf2-tutorial.loc

apache(httpd conf)有这个设置,有些人建议删除他的哈希标签,但我没有为我工作,所以我找到了它:

# Virtual hosts
# Include conf/extra/httpd-vhosts.conf

同样在同一个文件(httpd conf)中,我将 AllowOverride None 更改为 AllowOverride FileInfo,如下所示:

<Directory />
AllowOverride FileInfo
Require all denied
</Directory>

重新启动 xampp 并显示祝贺网页。

于 2014-03-16T22:22:03.387 回答