【问题标题】:Symfony2 and mod_rewrite URLs not workingSymfony2 和 mod_rewrite URL 不起作用
【发布时间】:2011-10-14 23:36:27
【问题描述】:

我在我的routing.yml 中配置了这种模式:

/{customer}/{site}/content/{id}.{format}

现在我想省略/{customer}/{site} 部分并用重写规则重写它。 Symfony 已经有一个带有重写规则的.htaccess。我修改为

RewriteRule ^(.*)$ app_dev.php/foo/bar/$1 [QSA,L]

但是当我将浏览器指向 http://localhost/content/42.html 时,我得到了这个异常:

No route found for "GET /content/42.html"

我摆弄了一下,发现为什么会出现这个问题:Symfony 使用 _SERVER['REQUEST_URI'] 变量来解析请求,但是当你使用 mod_rewrite 时,这个 var 中的 URL 不是预期的 /foo/bar/content/42.html 而是 @987654330 @。

我可以用我自己的替换 app_dev.php 并覆盖 REQUEST_URI 但我不想破解 Symfony。

编辑: 从路由配置中删除 /{customer}/{site} 部分不是解决方案,因为我正在处理多客户/多站点应用程序,我想通过 URL 隐藏这些部分,以便我可以将浏览器指向 www.my-fancy-site.com 和apache 将此 URL 重写为 /foo/bar/content/42.html

【问题讨论】:

    标签: mod-rewrite symfony


    【解决方案1】:

    你问的很好:

    做某事:

    app/config/routing.yml:
    
    blog:
      pattern: /{customer}/{site}/content/{id}.{format}
      defaults: { _controller: TestBundle:Blog:view ,customer:foo, site:bar }
    

    并且根本不要碰.htaccessapp_dev.php中的重写规则

    编辑:对于您更新的问题,您也需要这个:

    app/config/routing.yml:
    
    blog:
      pattern: /
      defaults: { _controller: TestBundle:Blog:view, customer:foo, site:bar, id:4 }
    

    但是这样 url 保持http://host/。我会使用一个欢迎控制器,然后简单地从该控制器重定向到另一条路线......一个重定向应该没问题。

    更准确地说:你需要两条路由和一个控制器来重定向到你的。

    请在此处进行 RTFM: http://symfony.com/doc/2.0/book/controller.html#route-parameters-controller-arguments

    这里: http://symfony.com/doc/2.0/book/routing.html

    【讨论】:

    • 感谢您的提示,但这对我不起作用,我澄清了我的问题。很抱歉这个令人困惑的问题。
    • @prehfeldt 我认为解决方案是正确的,它应该可以工作。实际问题不在于 URL 重写,而在于您定义路由的方式。我也对 $_SERVER["REQUEST_URI"] 感到困惑,但这实际上不是这里的问题。考虑到您的localhost/content/42.html 示例,一旦请求到达 app_dev.php 或 app.php,symfony 将查找路由 /content/42.html,因此您需要在 symfony 中定义此路由。
    猜你喜欢
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    相关资源
    最近更新 更多