Symfony:htaccess隐藏app.php或app_dev.php

时间:2013-05-22 10:49:01

标签: .htaccess symfony

我知道这已被问过很多次了,但有些奇怪的事发生在我身上。 Apache DocumentRoot指向 symfony / web / ,这是我在web / dir中的.htaccess:

DirectoryIndex app_dev.php
#DirectoryIndex app.php

<IfModule mod_rewrite.c>
    RewriteEngine On

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

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]


    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(.*)$ app_dev.php [QSA,L]
    #RewriteRule ^(.*)$ app.php [QSA,L]

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    RewriteRule .? %{ENV:BASE}app.php [L]
</IfModule>

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

嗯,问题是www.example.com/route1/正在运行,www.example.com/route2/正在抛出错误:

  

糟糕!发生错误   服务器返回“404 Not Found”。   有些东西坏了。请发送电子邮件至[email],告诉我们发生此错误时您在做什么。我们会尽快修复它。给您造成的任何不便,请原谅。

www.example.com/app_dev.php/route2/工作正常(同时www.example.com/app_dev.php/route1/

帮助?

更新。缓存清除prod抛出此错误(我从未尝试过,我正在开发dev):

  

[学说\共同\代理\异常\ UnexpectedValueException]   类“MyProject \ PanelBundle \ Entity \ User”中方法“addRole”中参数“userRoles”的类型提示无效。

     

[ReflectionException]   类Maycol \ BlogBu​​ndle \ Entity \ Role不存在

2 个答案:

答案 0 :(得分:6)

这是对我有用的.htaccess:

DirectoryIndex app.php
#DirectoryIndex app_dev.php

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Redirect to URI without front controller to prevent duplicate content
    # (with and without `/app.php`). Only do this redirect on the initial
    # rewrite by Apache and not on subsequent cycles. Otherwise we would get an
    # endless redirect loop (request -> rewrite to front controller ->
    # redirect -> request -> ...).
    # So in case you get a "too many redirects" error or you always get redirected
    # to the startpage because your Apache does not expose the REDIRECT_STATUS
    # environment variable, you have 2 choices:
    # - disable this feature by commenting the following 2 lines or
    # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
    #   following RewriteCond (best solution)
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    #RewriteRule ^app_dev\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
    RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]


    RewriteCond %{REQUEST_FILENAME} -f
    #RewriteRule ^(.*)$ app_dev.php [QSA,L]
    RewriteRule ^(.*)$ app.php [QSA,L]

    # The following rewrites all other queries to the front controller. 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
    # app.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}app_dev.php [L]
    RewriteRule .? %{ENV:BASE}app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the startpage to the front controller explicitly so that the website
        # and the generated links can still be used.
        #RedirectMatch 302 ^/$ /app_dev.php/
        RedirectMatch 302 ^/$ /app.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>

答案 1 :(得分:0)

此错误与Apache / htaccess无关。这个404错误页面是symfony本身的默认页面!

Something is broken. Please e-mail us at [email] and let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.

没有路由匹配/ route2。检查您的路由,如下所示:

php app/console router:debug 

要更快地检查它,请使用grep或findstr(Windows)

php app/console router:debug | grep route2

还请检查您的路由是否不仅在开发环境中配置(即仅在routing_dev.yml中)!

php app/console router:debug --env=prod

请用...清除您的制作缓存。

php app/console cache:clear --env=prod

...并检查您的日志文件是否存在未被捕获的异常,导致访问该页面时显示404页面。例如,您可以使用此命令查看生产日志文件中的实时更改。

tail -f app/logs/prod.log