Symfony - 在本地可访问路径 /admin 但在生产环境中为 404

Symfony - Path /admin accessible in local but 404 in prod environnement

我已经在我的网站上添加了 sonata admin

在本地,我可以访问路径 /admin,但当我将它推送到我的产品环境中时却不能。当我尝试访问它时,我得到一个 404 :

未找到 在此服务器上找不到请求的 URL /admin。

我已删除 var/cache/prod

中的缓存

以防万一我的 sonata_admin.yaml 的内容被使用:

_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /admin

admin_area:
    resource: "@SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
    prefix: /admin

sonata_user_admin_security:
    resource: '@SonataUserBundle/Resources/config/routing/admin_security.xml'
    prefix: /admin

sonata_user_admin_resetting:
    resource: '@SonataUserBundle/Resources/config/routing/admin_resetting.xml'
    prefix: /admin/resetting

我是否必须配置一些东西才能访问我服务器上的这些路径?或者这与 .htaccess 文件有什么关系吗?

任何可能导致此问题的线索?

提前感谢您的帮助!

更新: 这是我在本地主机上执行 debug:router 命令时所拥有的(无法在生产中执行)

D:\website>php bin/console debug:router
 ------------------------------------------ ---------- -------- ------ ----------------------------------------------------
  Name                                       Method     Scheme   Host   Path
 ------------------------------------------ ---------- -------- ------ ----------------------------------------------------
  homepage                                   ANY        ANY      ANY    /
  _twig_error_test                           ANY        ANY      ANY    /_error/{code}.{_format}
  _wdt                                       ANY        ANY      ANY    /_wdt/{token}
  _profiler_home                             ANY        ANY      ANY    /_profiler/
  _profiler_search                           ANY        ANY      ANY    /_profiler/search
  _profiler_search_bar                       ANY        ANY      ANY    /_profiler/search_bar
  _profiler_phpinfo                          ANY        ANY      ANY    /_profiler/phpinfo
  _profiler_search_results                   ANY        ANY      ANY    /_profiler/{token}/search/results
  _profiler_open_file                        ANY        ANY      ANY    /_profiler/open
  _profiler                                  ANY        ANY      ANY    /_profiler/{token}
  _profiler_router                           ANY        ANY      ANY    /_profiler/{token}/router
  _profiler_exception                        ANY        ANY      ANY    /_profiler/{token}/exception
  _profiler_exception_css                    ANY        ANY      ANY    /_profiler/{token}/exception.css
  webhook_index                              ANY        ANY      ANY    /mailchimp/webhook/endpoint
  admin_app_event_list                       ANY        ANY      ANY    /admin/app/event/list
  admin_app_event_create                     ANY        ANY      ANY    /admin/app/event/create
  admin_app_event_batch                      ANY        ANY      ANY    /admin/app/event/batch
  admin_app_event_edit                       ANY        ANY      ANY    /admin/app/event/{id}/edit
  admin_app_event_delete                     ANY        ANY      ANY    /admin/app/event/{id}/delete
  admin_app_event_show                       ANY        ANY      ANY    /admin/app/event/{id}/show
  admin_app_event_export                     ANY        ANY      ANY    /admin/app/event/export
  admin_sonata_user_user_list                ANY        ANY      ANY    /admin/sonata/user/user/list
  admin_sonata_user_user_create              ANY        ANY      ANY    /admin/sonata/user/user/create
  admin_sonata_user_user_batch               ANY        ANY      ANY    /admin/sonata/user/user/batch
  admin_sonata_user_user_edit                ANY        ANY      ANY    /admin/sonata/user/user/{id}/edit
  admin_sonata_user_user_delete              ANY        ANY      ANY    /admin/sonata/user/user/{id}/delete
  admin_sonata_user_user_show                ANY        ANY      ANY    /admin/sonata/user/user/{id}/show
  admin_sonata_user_user_export              ANY        ANY      ANY    /admin/sonata/user/user/export
  admin_sonata_user_group_list               ANY        ANY      ANY    /admin/sonata/user/group/list
  admin_sonata_user_group_create             ANY        ANY      ANY    /admin/sonata/user/group/create
  admin_sonata_user_group_batch              ANY        ANY      ANY    /admin/sonata/user/group/batch
  admin_sonata_user_group_edit               ANY        ANY      ANY    /admin/sonata/user/group/{id}/edit
  admin_sonata_user_group_delete             ANY        ANY      ANY    /admin/sonata/user/group/{id}/delete
  admin_sonata_user_group_show               ANY        ANY      ANY    /admin/sonata/user/group/{id}/show
  admin_sonata_user_group_export             ANY        ANY      ANY    /admin/sonata/user/group/export
  sonata_admin_redirect                      ANY        ANY      ANY    /admin/
  sonata_admin_dashboard                     ANY        ANY      ANY    /admin/dashboard
  sonata_admin_retrieve_form_element         ANY        ANY      ANY    /admin/core/get-form-field-element
  sonata_admin_append_form_element           ANY        ANY      ANY    /admin/core/append-form-field-element
  sonata_admin_short_object_information      ANY        ANY      ANY    /admin/core/get-short-object-description.{_format}
  sonata_admin_set_object_field_value        ANY        ANY      ANY    /admin/core/set-object-field-value
  sonata_admin_search                        ANY        ANY      ANY    /admin/search
  sonata_admin_retrieve_autocomplete_items   ANY        ANY      ANY    /admin/core/get-autocomplete-items
  sonata_user_admin_security_login           ANY        ANY      ANY    /admin/login
  sonata_user_admin_security_check           POST       ANY      ANY    /admin/login_check
  sonata_user_admin_security_logout          ANY        ANY      ANY    /admin/logout
  sonata_user_admin_resetting_request        GET        ANY      ANY    /admin/resetting/request
  sonata_user_admin_resetting_send_email     POST       ANY      ANY    /admin/resetting/send-email
  sonata_user_admin_resetting_check_email    GET        ANY      ANY    /admin/resetting/check-email
  sonata_user_admin_resetting_reset          GET|POST   ANY      ANY    /admin/resetting/reset/{token}
 ------------------------------------------ ---------- -------- ------ ----------------------------------------------------

找到解决方案:

Symfony 4.x中,我们需要指定一个特定的重写规则。

我关注了Symfony 4 documentation。 我只需要执行此命令即可修复它:

composer require symfony/apache-pack

您也可以将此代码添加到 Symfony 项目的 public 子目录中的 .htaccess 文件中:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

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

瞧瞧! /admin 并且所有子路径现在都可以在服务器上访问。