Mod rewrite Symfony2和mod_重写URL不起作用

Mod rewrite Symfony2和mod_重写URL不起作用,mod-rewrite,symfony,Mod Rewrite,Symfony,我在我的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

我在我的
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使用
\u SERVER['REQUEST\u URI']
变量来解析请求,但是当您使用mod\u rewrite时,这个变量中的URL不是预期的
/foo/bar/content/42.html
,而是
content/42.html

我可以用自己的
app\u dev.php
替换它,并覆盖
REQUEST\u URI
,但我不想攻击Symfony

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

很好,你问了:

做某事,例如:

app/config/routing.yml:

blog:
  pattern: /{customer}/{site}/content/{id}.{format}
  defaults: { _controller: TestBundle:Blog:view ,customer:foo, site:bar }
而且根本不要触碰
.htaccess
app_dev.php
中的重写规则

编辑:对于更新的问题,您也需要以下内容:

app/config/routing.yml:

blog:
  pattern: /
  defaults: { _controller: TestBundle:Blog:view, customer:foo, site:bar, id:4 }
但这样url就会保持
http://host/
。我会使用一个欢迎控制器,然后简单地从该控制器重定向到另一条路径。。。一个重定向应该可以

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

请点击此处:

在这里:

谢谢你的提示,但这对我不起作用,我澄清了我的问题。很抱歉,这个问题让人困惑。@prehfeld我认为解决方案是正确的,它应该会起作用。实际的问题不是URL重写,而是定义路由的方式。我对$\u SERVER[“REQUEST\u URI”]也感到困惑,但这实际上不是问题所在。考虑到您的示例,一旦请求点击app_dev.php或app.php,symfony将查找route/content/42.html,因此您需要在symfony中定义此路由。