【问题标题】:Symfony routing - How can I implement a rule that redirects any URL that matches a specific pattern?Symfony 路由 - 如何实现重定向任何与特定模式匹配的 URL 的规则?
【发布时间】:2011-12-25 05:00:52
【问题描述】:

假设我希望将包含单词“potatoes”的所有 URL 重定向到 http://www.mysite.com/home

是否有任何智能路由规则可以实现? 还是应该使用过滤器?

PS:我使用的是 Symfony 1.4

【问题讨论】:

    标签: regex url redirect symfony1 routing


    【解决方案1】:

    我认为这可能可以通过编写自己的sfRoute 类并尝试覆盖matchesUrl() 函数来实现。但这需要对 symfony 有相当深入的了解。

    可能只是添加一个 .htaccess 重写规则更容易,像这样(未测试):

    RewriteRule ^(.*)potatoes(.*)$ index.php/home [R=301]
    

    这当然只有在你的网络服务器是 Apache 时才有效,否则你必须为你的网络服务器找到一个等效的。

    【讨论】:

    • 嗯,我喜欢使用 .htaccess 文件的想法。但实际上,我需要变量才能将mysite/potatoes/whateverthereisafter 重定向到mysite/home/whateverthereisafter。你能在 .htaccess 中做到这一点吗?
    • 好的,你可以使用 $1 来捕捉括号中的内容。
    • Ehm...您说“potatoes 在 URL 中的任何位置”...如果您的 URL 具有类似 mysite/potatoes/{something} 的结构,您可能应该使用 symfony 路由系统。您可以在您的 routing.yml 中创建一个规则,然后重定向到相应的操作 (url)。
    猜你喜欢
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    相关资源
    最近更新 更多