htaccess& Symfony,一条路由导致404,但适用于app_dev.php

时间:2016-02-01 09:03:01

标签: php apache .htaccess symfony

我有一个管理区域,我按照here所述登录。 此表单适用于开发环境,但当我切换到生产时,/ admin路由返回404。

控制器(404不使用app_dev.php时):

/**
 * @Route("/admin", name="mainPage")
 */
public function indexAction(Request $request)
{
    return $this->render('admin/index.html.twig', array());
}

我的.htaccess in / web(删除了评论的可读性)

DirectoryIndex app.php
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]


RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]


RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]


RewriteRule ^ %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 302 ^/$ /app.php/
    </IfModule>
</IfModule>

最后,我的000-default.conf

DocumentRoot /var/www/html/web

<Directory /var/www/html/web>
    AllowOverride All
    Allow from All
</Directory>

错误在哪里? /admin/login路由显示登录表单。登录后,我被移动到admin/(带有404),但是当我手动输入另一个受保护的地址时,它会起作用(例如admin/setup/prices),因为我实际登录了。

例如,将路由从/admin更改为/admin/start不会改变任何内容。

如果您需要security.yml:

# To get started with security, check out the documentation:
# http://symfony.com/doc/current/book/security.html
security:

encoders:
  Symfony\Component\Security\Core\User\User: plaintext
  AppBundle\Entity\User: bcrypt

# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
    in_memory:
        memory: ~
    our_db_provider:
        entity:
            class: AppBundle:User
            property: username

access_control:
    - { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin, roles: ROLE_ADMIN }
firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        anonymous: ~
        # activate different ways to authenticate

        http_basic: ~
        # http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate

        provider: our_db_provider

        form_login:
            login_path: login_route
            check_path: login_check
            csrf_token_generator: security.csrf.token_manager
            use_referer: true
        logout:
            path:   logout_route
            target: mainPage
        # http://symfony.com/doc/current/cookbook/security/form_login_setup.html

UPDATE 我现在看到/admin实际上已被调用,但获得301 /admin/

所以我认为问题出在htaccess的这一行:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

将控制器中的路线更改为/admin/无济于事。

1 个答案:

答案 0 :(得分:0)

在prod env&#39;中,也许您可​​以将有关错误的生产日志分享给我们?

否则,你是否试图制定具体的消防规则?

firewalls:
    login_firewall:
        pattern:   ^/login$
        anonymous: ~
    secured_area:
        pattern:    ^/
        form_login: ~