Zend Framework 2 Htaccess

左心房为你撑大大i 提交于 2020-01-05 12:34:25

问题


Hello guys i have problem in ZF2 with htaccess.

I create vhost and all work good. when i call

my-vhost.localhost all work good but when i add some uri segment like /index.php or /1234 I get 404.

I edit .htaccess in public dir and put

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

Bur again i have 404. Any idea how i can i slow this?

My full .htaccess is :

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]

VHOST

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

Who think problem is routing check my route mod.

<?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',
        ),
    ),
);

回答1:


The contents of the shipped .htaccess file are as follows;

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]

It differs very slightly from your copy, I have tried your .htaccess file on a working example and it appears fine.

Is the 404 you are getting a generic "server looking" 404 or is it a ZF2 themed page?

Might be a silly suggestion but have you tried http://my-vhost.localhost/public or http://my-vhost.localhost/public/album ???

As far as I know you shouldnt need to call .php files directly




回答2:


Im not sure it is too late to answer this question, as Finbarr suggest to try with http://my-vhost.localhost/public or http://my-vhost.localhost/public/album.

I had also the same problem everytime I type www.zf2-tutorial.loc it always was redirected to xampp main page.

Finally i read the comments froom Finbarr i changed to

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

after that the webspage: Welcome to Zend Framework 2 was loaded.

#the virtual host:

NameVirtualHost *: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>

in the host I made several combinations trying to find out which one was correct.

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

the apache (httpd conf) has this settings some people suggest to delete he hash tag but I didn't work for me so left it as i found it:

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

Also in the same file (httpd conf) I change the AllowOverride None to AllowOverride FileInfo as follows:

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

re started the xampp and it was displayed the congratulations web page.



来源:https://stackoverflow.com/questions/18341334/zend-framework-2-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!