【问题标题】:Oops! An Error Occurred The server returned a "404 Not Found". Symfony哎呀!发生错误 服务器返回“404 Not Found”。 Symfony
【发布时间】:2017-04-15 12:03:47
【问题描述】:

谁能告诉我在symfony中导致这个问题的原因以及如何解决它。

哎呀!发生错误服务器返回“404 Not Found”。

我只是 symfony 和 cpanel 的新手。

我刚刚将我的工作 SF 应用程序从我的电脑 (localhost) 上传到我的服务器或CPANEL。 CPanel 没有SSH access,所以我使用 FTP 上传所有目录。文件目录如下。

主页/swipecom

  • 缓存
  • 非接触式(SF 目录所在的位置)
  • public_html(/web 唯一的 SF 目录)
  • 变量
  • ssl
  • tmp
  • public_ftp
  • 日志

我创建了.htaccesspublic_html 就像这样public_html/.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /web

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>

最后,我用类似这样的方式配置了app.php

<?php

use Symfony\Component\HttpFoundation\Request;

/** @var \Composer\Autoload\ClassLoader $loader */

// $loader = require __DIR__.'/../app/autoload.php';
// include_once __DIR__.'/../var/bootstrap.php.cache';

$loader = require '/home2/swipecom/contactless/app/autoload.php';
include_once '/home2/swipecom/contactless/var/bootstrap.php.cache';

$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);

// When using the HttpCache, you need to call the method in your front controller instead of relying on the configuration parameter
//Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

当我尝试像 www.domain.com 这样访问应用程序时,这是我收到的回复。

哎呀!发生错误

服务器返回“404 Not Found”。

有些东西坏了。请让我们知道您在此期间在做什么 错误发生。我们会尽快修复它。对不起任何 造成的不便。

更新

routing.yml

route_frontend:
    resource: "@SwipeBundle/Resources/config/routing_frontend.yml"

route_backend:
    resource: "@SwipeBundle/Resources/config/routing_backend.yml"

route_security:
    resource: "@SwipeBundle/Resources/config/routing_security.yml"

routing_dev.yml

_wdt:
    resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
    prefix:   /_wdt

_profiler:
    resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
    prefix:   /_profiler

_errors:
    resource: "@TwigBundle/Resources/config/routing/errors.xml"
    prefix:   /_error

_main:
    resource: routing.yml

AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            //  new SunCat\MobileDetectBundle\MobileDetectBundle(),
            new SwipeBundle\SwipeBundle(),
        ];

        if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
            $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        }

        return $bundles;
    }

    public function getRootDir()
    {
        return __DIR__;
    }

    public function getCacheDir()
    {
        return dirname(__DIR__).'/var/cache/'.$this->getEnvironment();
    }

    public function getLogDir()
    {
        return dirname(__DIR__).'/var/logs';
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
    }
}

.htaccessweb 目录中

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}/$2 [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>

【问题讨论】:

  • 它看起来工作正常,你有注册到你的应用程序的路由吗?
  • 感谢您的评论,我在上面添加了我的路由。
  • 上传前是否生成了生产缓存?如果是这样,请更改 new AppKernel('prod', false);到新的 AppKernel('prod', true);可能会得到更好的错误信息。并检查您的主机是否有任何关于 symfony 的说明。经常会出现目录权限问题。
  • 在上传之前我只是运行这个命令./bin/console cache:clear --env=prod &amp; ./bin/console cache:clear --env=dev,

标签: php symfony


【解决方案1】:

我认为这是对你有帮助的教程:

sudo a2enmod rewrite

Enabling mod_rewrite

.htaccess

.htaccess 文件:

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}/$2 [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>

https://www.digitalocean.com/community/tutorials/how-to-rewrite-urls-with-mod_rewrite-for-apache-on-ubuntu-16-04

【讨论】:

  • 我必须覆盖web 目录中的.htaccess 吗?
  • @phpmeter 这个 .htaccess 文件在 web 目录中。
  • 我确实更新了我的web 目录中的.htaccess,但同样的问题。
猜你喜欢
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
  • 2012-09-20
  • 2018-04-17
  • 2016-09-02
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
相关资源
最近更新 更多