【问题标题】:Rewrite url in Silex PHP Framework在 Silex PHP 框架中重写 url
【发布时间】:2017-10-01 13:17:58
【问题描述】:

我是 Silex Framework 的新手,但我遇到了 url 重写的问题。

问题:

当我在浏览器中输入这个 -> http://localhost/mysite/ 它正在工作并加载索引视图。

$app->get('/', function() use($app) {

    return $app['twig']->render('index.twig.html');

});

但是当我输入http://localhost/mysite/about

$app->get('/about', function() use($app) 
{ 
    return $app['twig']->render('about.twig.html');
});

它给了我以下错误:The requested URL http://localhost/mysite/about was not found on this server.

如果我输入这个网址http://localhost/mysite/index.php/about

这个网址有效,我不明白为什么?我需要在没有这个 index.php 的 url 中加载这个视图。我该怎么做?提前致谢。

【问题讨论】:

    标签: url-rewriting silex


    【解决方案1】:

    将其用作.htaccess 文件的内容(基于 Symfony 标准版提供的文件)。

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
        RewriteRule ^(.*) - [E=BASE:%1]
        RewriteCond %{ENV:REDIRECT_STATUS} ^$
        RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
        RewriteCond %{REQUEST_FILENAME} -f
        RewriteRule .? - [L]
        RewriteRule .? %{ENV:BASE}/index.php [L]
    </IfModule>
    

    【讨论】:

    • 非常感谢您的回复,但它给了我同样的错误“未找到请求的 URL /mysite/about 在此服务器上找不到。”
    • 我认为 .htaccess 无法正常工作 .htaccess 的任何内容都没有变化 可能需要对 apache 配置进行一些更改?
    • 很可能是 apache 重写模块未启用,或者 .htaccess 文件不被允许(即 AllowOverride All 未在 Apache 主配置中设置)
    【解决方案2】:

    您在子目录中使用 Silex:

    http://localhost/mysite/index.php/about
    

    因此您必须调整 .htaccess 的 RewriteBase,试试这个:

    <IfModule mod_rewrite.c>
      Options -MultiViews
      RewriteEngine On
      RewriteBase /mysite
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^ index.php [QSA,L]
    </IfModule>
    

    如果您的服务器运行的是 Apache >= 2.2.16,您可以使用 FallbackResource 代替上面的代码,试试这行代码:

    FallbackResource /mysite/index.php
    

    【讨论】:

      【解决方案3】:

      问题出在 Apache 的 rewrite_mode 中。我更改了 Apache 默认配置 -> “AllowOverride All”

      http://www.jarrodoberto.com/articles/2011/11/enabling-mod-rewrite-on-ubuntu

      我添加了 .htaccess 文件。它正在工作,谢谢大家!

      【讨论】:

        猜你喜欢
        • 2010-12-15
        • 1970-01-01
        • 2016-04-04
        • 1970-01-01
        • 2012-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-12
        相关资源
        最近更新 更多