【问题标题】:URL re-write not working with my Apache using PHP Slim Framework?使用 PHP Slim 框架的 URL 重写不适用于我的 Apache?
【发布时间】:2013-12-07 07:57:21
【问题描述】:

好的,我不确定这是否是我,但我刚刚遇到了用于 PHP 的 Slim 框架,它看起来非常简单且易于使用更好的应用程序。我确定它是我的 .htaccess 文件,但我不确定我到底做错了什么。

所以这适用于 Slim:

  http://testvm/index.php/hello/4412

但是这个

   http://testvm/hello/4412

给我一​​个未找到的错误,来自 Apache,

这个

   http://testvm/

从 Slim 返回 404 错误(或者至少我认为它是来自 slim 的 404)。

这对于 Server Fault 来说可能是一个更好的问题,但它可能是我设置 Slim 的方式,我对此和 PHP Composer 非常陌生,我曾经使用它来安装 Slim。

这是我的 index.php 文件

        <!doctype html><html>
        <head><meta charset="utf-8"><title>Untitled Document</title></head> <body>
        <?php
            require 'vendor/autoload.php';

            $app = new \Slim\Slim();

            $app->get('/hello/:name', function ($name) {
                    echo "Hello, $name";
            });

            $app->run();
       ?>
       </body></html>

这是我当前的 .htaccess 文件

       Options +FollowSymLinks
       RewriteEngine On
       RewriteBase /

       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
       RewriteCond %{REQUEST_FILENAME}.php -f
       #RewriteRule    ^$ /testvm/index.php/  [L]
       #RewriteRule    (.*) /testvm/index.php/$ [L]

如果我取消注释底部的两条规则,我会收到服务器 500 错误?

我已经试过了

         RewriteEngine On
         RewriteBase /my_app/

         RewriteCond %{REQUEST_FILENAME} !-f
         RewriteRule ^ index.php [QSA,L]

这使“/testvm/index.php/hello/XXXX”工作,但我希望它在 URL 中没有任何 index.php 文件的情况下工作?我确信这与我的 .htaccess 文件有关。

现在我在一个虚拟机(虚拟机)中运行它,它是 Ubuntu 13.04,它已经完全更新到 13.10 let。我已经检查过了,rewrite.load 列在我在这个 VM 上构建的 apache 的模式启用文件夹中。

那么我做错了什么?

欢迎大家帮忙。 :)

格伦。

【问题讨论】:

    标签: apache .htaccess mod-rewrite slim


    【解决方案1】:

    你能在DOCUMENT_ROOT/testvm/.htaccess文件中试试这个规则吗:

       RewriteEngine On
       RewriteBase /
    
       RewriteRule ^$ /testvm/index.php  [L]
    
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteRule ^testvm/((?!index\.php/).+)$ /testvm/index.php/$1 [L,NC]
    

    【讨论】:

    • 试试这个,但它也给我一个500服务器错误,这是否意味着mod重写不能正常工作?我应该卸载 Apache 并尝试 Nginx 吗?另外 /testvm/ 只是 web 指针,它加载保存到 /var/www 路径的内容,所以我的 .htaccess 文件位于 doc 根目录中,或者我不明白吗?
    • ok 尝试编辑代码,如果它给出 500,你需要检查你的 Apache error.log
    • 刚刚尝试了您的编辑,但它刚刚返回来自 Apache 而不是 Slim 的 404 错误。
    • 仅供测试尝试:RewriteRule ^testvm/((?!index\.php/).+)$ /testvm/index.php/$1 [L,NC,R]
    • 对不起,不,我认为这可能与我的 Apache 安装有关,我可能会构建一个干净的新构建并安装 Nginx 以查看它是否更容易
    【解决方案2】:

    我已经标记出你的答案是正确的,因为你给了我很多帮助,我已经用下面的代码让它工作了,

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /index.php [QSA]
    

    所以现在如果我去 testvm/ 我得到一个苗条的 404 错误并且在加载 testvm/hello/TESTNAMEEXAMPLE 时它会加载。

    希望我上面的代码对其他人有所帮助。

    格伦。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-04
      • 2015-12-08
      • 2018-01-05
      • 1970-01-01
      • 2020-02-09
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多