7

我正在尝试设置和学习 PHP 的 Fat Free 框架。 http://fatfree.sourceforge.net/

设置起来相当简单,我正在使用 MAMP 在我的机器上运行它。

我能够让“hello world”示例运行正常:

require_once 'path/to/F3.php';
F3::route('GET /','home');
    function home() {
        echo 'Hello, world!';
    }
F3::run();

但是当我尝试添加第二部分时,它有两条路线:

require_once 'F3/F3.php';

F3::route('GET /','home');
function home() {
    echo 'Hello, world!';
}

F3::route('GET /about','about');
function about()
{
    echo 'About Us.';
}

F3::run();

如果我尝试第二个 URL:/about,我会收到 404 错误

不知道为什么其中一个mod_rewrite命令会起作用,而另一个则不起作用。

下面是我的.htaccess文件:

# Enable rewrite engine and route requests to framework
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA]

# Disable ETags
Header Unset ETag
FileETag none

# Default expires header if none specified (stay in browser cache for 7 days)
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A604800
</IfModule>
4

8 回答 8

11

所以我的朋友实际上帮助我解决了这个问题。我遇到了完全相同的问题,但基本上我也在使用 MAMP,并且我所有的 Fat Free 文件都在fatfreeMAMP 的 htdocs 内的一个目录中。

解决方案是您需要修改RewriteBaseto 指向/[dirname]/而不是仅仅/然后更改RewriteRule/[dirname]/index.php.

我的 .htaccess 看起来像这样:

RewriteEngine On
RewriteBase /fatfree/
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /fatfree/index.php [L,QSA]

设置好之后,您可以完全按照 Fat Free 文档中的示例进行操作,它会像魅力一样工作。这让我难倒了一段时间,这就是它所需要的。此外,如果您使用的是 MAMP,请在其中编辑httpd.conf文件/Applications/MAMP/conf/apache并确保更改以下内容:

<Directory />
    Options Indexes FollowSymLinks
    AllowOverride None
</Directory>

<Directory />
    Options Indexes FollowSymLinks
    AllowOverride All
</Directory>

基本上None改成All.

于 2011-02-21T19:31:47.253 回答
2

如果您在子文件夹下运行 F3,则必须更改 .htaccess 中的 RewriteBase 以匹配该文件夹。

于 2011-01-17T10:24:08.340 回答
1

Note: If you want to call your .htaccess file something else, you can change the name of the file using the AccessFileName directive. For example, if you would rather call the file .config then you can put the following in your server configuration file:

AccessFileName .config  

here

于 2011-10-20T00:33:33.747 回答
1

我为此头疼了2天。我调试了htaccess和php。实际问题是这样的:

如果您从他们的 fatfree-1.4.4 zip 下载中复制了 .htaccess 文件,则它不是.htaccess它的htaccess(. 丢失)只需将此文件从 htaccess 重命名为 .htaccess ,一切都会像文档中提到的那样完美运行! !

我正在使用这个 .htaccess 也适用于非根文件夹

Options -Indexes 
IndexIgnore *    

RewriteEngine On
RewriteCond $1 !^(index\.php|images|main\.css|form\.css|favicon\.ico|robots\.txt|sitemap\.xml)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
于 2011-02-11T13:50:09.107 回答
1

在您的 .htaccess 中,您有 'index.php' 它需要一个斜杠 ... '/index.php'

# Enable rewrite engine and route requests to framework
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-l          
RewriteCond %{REQUEST_FILENAME} !-f          
RewriteCond %{REQUEST_FILENAME} !-d          
RewriteRule .* /index.php [L,QSA]    

否则,当它尝试重写 /about/ 时,它将寻找 /about/index.php 而不仅仅是根 /index.php


我只是有另一个想法..虽然安装了 mod_rewrite ,但服务器可能有一个怪癖导致它不重写..

如果下面的全局路由不起作用,您可能需要测试重写

RewriteRule ^/google http://www.google.com [L,NC];

您还可以尝试目录的全局路由

F3::route('GET /about/*','about');

但这意味着 domain.com/about/ 下的任何东西......任何东西......都会重新路由到 about 功能......


关于 mod_rewrite 和 FF 的说明

正如你所说,FF给你一个404,因为它期待'/'而不是'/index.php'......但是,它是index.php期待的差异......

为了证明这一点,我相信你可以复制你的

F3::route('GET /','home');

作为

F3::route('GET /index.php','home');

页面应该显示...

这样做的原因是如果你只是去 / 目录(或 /index.php)eitehr 方式 apache 服务x index.php 页面....

mod_rewrite 允许您重定向 /about 并将其重定向到 index.php .. 因此,如果您的重写规则不起作用,则重定向/重写不会发生,您将获得 404...

正如我上面提到的,使用 google 规则测试 mod_rewrite .. 然后尝试转到http://localhost:80/google 如果它没有将您重定向到 google 则您的重写引擎无法正常工作...(可能是 windows 配置的问题..)


在 windows 下启用 mod_rewrite: 打开你的 http.conf 找到这一行:

#LoadModule rewrite_module modules/mod_rewrite.so

从行中删除注释标记 (#)... 这样您就有了: LoadModule rewrite_module modules/mod_rewrite.so 保存文件并重新启动 apache..

或者..我想你可以说:

LoadModule rewrite_module modules/mod_rewrite.so

在您的 htaccess 文件的开头...

于 2011-01-03T08:02:56.603 回答
0

我正在使用 MAMP 堆栈运行它。我将他们的文件夹从“fatfree-master”重命名为“f3”。我把那个文件夹放在 htdocs 旁边。他们的 .htaccess 文件(现在在 MAMP/f3/lib 中)保持不变。

根据他们的示例,我的 .htaccess (在我的 web 子文件夹中)是库存标准:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L,QSA]

希望这可以帮助某人。

于 2014-07-02T23:01:36.937 回答
0

这个回复对你来说可能为时已晚,但我昨天遇到了同样的问题。

听起来问题是apache没有重写url。尝试在 OSX 10.7 上运行 F3 时我遇到了同样的问题 - ' GET /' 路由可以工作,但 ' GET /foo' 因为 F3 index.php 位于 localhost/F3 的子目录中,所以不能工作。我的解决方案是:

  1. 确保 .htaccess 已按您的方式设置。
  2. 确保在 apache 的 httpd.conf 中启用了 mod_rewrite.so
  3. 确保您AllowOverride All在 httpd.conf (文件下方)中为您的 Web 目录设置(我的是 None)。
  4. 重启阿帕奇

如果没有第 3 步,apache 将忽略任何重写指令。我通过更改本地 wordpress 安装上的永久链接发现了这一点,但它们未能表明问题是 apache 配置,而不是 F3。

于 2011-07-11T23:06:54.670 回答
0

我有同样的问题,我通过将.htaccess文件移动到 fatfree 项目的根目录来解决它。

在根目录下使用以下代码创建文件.htaccess

# Enable rewrite engine and route requests to framework
RewriteEngine On

# Some servers require you to specify the `RewriteBase` directive
# In such cases, it should be the path (relative to the document root)
# containing this .htaccess file
#
# RewriteBase /

RewriteCond %{REQUEST_URI} \.ini$
RewriteRule \.ini$ - [R=404]

RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
于 2021-01-07T01:51:27.310 回答