【问题标题】:Symfony: htaccess to hide app.php or app_dev.phpSymfony:htaccess 隐藏 app.php 或 app_dev.php
【发布时间】:2013-05-17 09:21:40
【问题描述】:

我知道这个问题已经被问过很多次了,但是有些奇怪的事情发生在我身上。 Apache DocumentRoot 指向 symfony/web/,这是我在 web/ 目录中的 .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 中的缓存清除会引发此错误(我以前从未尝试过,我正在开发):

[Doctrine\Common\Proxy\Exception\UnexpectedValueException] “MyProject\PanelBundle\Entity\User”类的“addRole”方法中参数“userRoles”的类型提示无效。

[反射异常] 类 Maycol\BlogBu​​ndle\Entity\Role 不存在

【问题讨论】:

    标签: .htaccess symfony


    【解决方案1】:

    这是对我有用的 .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>
    

    【讨论】:

    • 出于某种原因,RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L] 仅适用于 /app.php/ 案例,不适用于 app.php。我不得不复制这些行并添加一个RewriteRule ^app\.php$ %{CONTEXT_PREFIX}/ [R=301,L] 条目。不知道为什么正则表达式有效!
    • 你能展示你的最终文件吗?我无法让它在生产服务器(app.php)上工作
    【解决方案2】:

    此错误与 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
    

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

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

    请使用 ... 清除您的生产缓存

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

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

    tail -f app/logs/prod.log
    

    【讨论】:

    • 我可以看到 route2 与 router:debug (/route2/) 并且似乎与 /route1/ 完全相同(名称除外),此外,/app_dev.php/route2/ 有效。
    • 请检查 router:debug 与 --env=prod ... app.php 仅使用 routing.yml 并且 app_dev.php 也加载 routing_dev.yml。如果路由仅在 routing_dev.yml 中,则在生产环境中不可用。
    • 它们在 dev 和 prod 两个环境中都是相同的,我看不出 route1 和 route2 之间有任何区别
    • 更新了我的答案...请在发出请求后清除缓存并检查日志文件。
    • 缓存清除抛出错误(将更新原始帖子),prod.log 有这个:[2013-05-22 13:05:17] request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel \Exception\NotFoundHttpException:“在 C:\wwwroot2\myproject\app\cache\prod\classes.php 第 3609 行 [] [] 处找不到“GET /route2/”的路由。顺便说一句,我在 Windows 下
    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 2015-02-12
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多