Fat Free 框架中 mod_rewrite 的简单问题
我正在尝试设置和学习 PHP 的 Fat Free 框架。 http://fatfree.sourceforge.net/
设置起来相当简单,我正在我的机器上运行它使用MAMP。
我能够让“hello world”示例运行 fin:
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,我会收到 404 错误:/about
不确定为什么其中一个 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>
I am trying to setup and learn the Fat Free Framework for PHP.
http://fatfree.sourceforge.net/
It's is fairly simple to setup and I am running it on my machine using MAMP.
I was able to get the 'hello world' example running just fin:
require_once 'path/to/F3.php';
F3::route('GET /','home');
function home() {
echo 'Hello, world!';
}
F3::run();
But when I try to add in the second part, which has two routes:
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();
I get a 404 error if I try the second URL: /about
Not sure why one of the mod_rewrite
commands would be working and not the other.
Below is my .htaccess
file:
# 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
所以我的朋友实际上帮我解决了这个问题。我遇到了完全相同的问题,但基本上我也使用 MAMP,并将所有无脂肪文件放在 MAMP htdocs 内的
fatfree
目录中。解决方案是您需要将
RewriteBase
修改为指向/[dirname]/
而不仅仅是/
,然后更改RewriteRule< /code> 到
/[dirname]/index.php
。我的 .htaccess 看起来像这样:
设置之后,您可以完全按照 Fat Free 文档中的示例进行操作,它会像魅力一样工作。这让我困惑了一段时间,但这就是所需要的。另外,如果您使用的是 MAMP,请编辑
/Applications/MAMP/conf/apache
中的httpd.conf
文件,并确保更改以下内容:基本上
更改 <代码>无到
全部
。So my friend actually helped me out with this issue. I ran into the exact same problem, but basically I'm using MAMP also and have all my Fat Free files within a
fatfree
dir within htdocs of MAMP.The solution is you need to mod the
RewriteBase
to point to/[dirname]/
instead of just/
and then changeRewriteRule
to/[dirname]/index.php
.My .htaccess looks like this:
After that's set, you can follow the example in the Fat Free doc exactly and it'll work like a charm. This stumped me for a while and this was all it needed. Also, if you're using MAMP, edit the
httpd.conf
file in/Applications/MAMP/conf/apache
and be sure to alter the following:to
Basically change
None
toAll
.如果您在子文件夹下运行 F3,则必须更改 .htaccess 中的 RewriteBase 以匹配该文件夹。
If you're running F3 under a subfolder, you must change the RewriteBase in .htaccess to match the folder.
在您的 .htaccess 中,您有“index.php”,它需要一个斜杠...“/index.php”,
否则当它尝试重写 /about/ 时,它将查找 /about/index.php 而不仅仅是根 /index .php
我只是有另一个想法.. 虽然安装了 mod_rewrite ,但服务器可能有一个怪癖,导致它无法重写。..
如果下面的全局路由不起作用,你可能想测试重写,
你可以也尝试目录的全局路由,
但这意味着domain.com/about/下的任何内容......任何内容......都会重新路由到about函数...
关于mod_rewrite和FF的注释
正如你所说,FF是给你一个 404 因为它期待 '/' 而不是 '/index.php'...但是,它是 index.php 期待差异..
为了证明这一点,我相信你可以复制你的
as
和页面应该显示...
这样做的原因是,如果您只是转到 / 目录(或 /index.php),apache 会以任何方式提供 index.php 页面...
mod_rewrite 允许您重定向 /about 并具有它重定向到index.php..因此,如果您的重写规则不起作用,则重定向/重写不会发生,您将得到404...
正如我上面提到的,使用google规则测试mod_rewrite..然后尝试转到
http://localhost:80/google
如果它没有将您重定向到谷歌,那么您的重写引擎无法工作...(可能是Windows配置的问题..)
在Windows下启用mod_rewrite:
打开你的 http.conf
找到这一行:
从该行中删除注释标记(#)...,这样你就有了:
LoadModule rewrite_module 模块/mod_rewrite.so
保存文件并重新启动 apache..
或者..我想你可以说:
在 htaccess 文件的开头...
In your .htaccess you have 'index.php' it needs a slash ... '/index.php'
otherwise when it tries to rewrite /about/ it will look for /about/index.php instead of just the root /index.php
I just had another thought.. it 'is' possible that althought mod_rewrite is intalled there may be a quirk with the server causing it not to rewrite..
If the global route below doesnt work you might want to test the rewrite
You could also try a global route for the directory
but that means anythin under domain.com/about/ ...... anything ... will reroute to the about function...
A note about mod_rewrite and FF
As you said, FF is givikng you a 404 because it is expecting '/' instead of '/index.php'... However, it is the index.php which is expecting the difference..
To demonstrate that, i believe you can duplicate your
as
and the page should display...
The reason for this is if you just go to the / directory (or /index.php) eitehr way apache servesx the index.php page....
The mod_rewrite allows you to redirect the /about and have it redirect to the index.php.. So if your rewrite rule is not working then the redirect/rewrite does not happen and you will get a 404...
As i mentioned above, test the mod_rewrite with the google rule.. then try to go to
http://localhost:80/google
if it does not redirect you to google then your rewrite engine is not working... (probably an issue with the windows configuration..)
to enable mod_rewrite under windows:
Open your http.conf
Find this line:
remove the comment mark (#) from the line... so you have:
LoadModule rewrite_module modules/mod_rewrite.so
Save the file and restart apache..
Alternatly.. I think you can just say:
at the start of your htaccess file...
我为此苦恼了两天。我调试了 htaccess 和 php 。实际问题是这样的:
如果您从 fatfree-1.4.4 zip 下载中复制了 .htaccess 文件,它不是 .htaccess 而是 htaccess (缺少 .)只需重命名将此文件从 htaccess 复制到 .htaccess,一切都会按照文档中提到的那样完美运行!
我正在使用这个 .htaccess 也适用于非根文件夹
I banged my head on this for 2 days. I debugged htaccess and php both. The actual problem is this :
If you copied the .htaccess file from their fatfree-1.4.4 zip download, its not .htaccess its htaccess (. is missing) Just rename this file to .htaccess from htaccess and everything will work perfectly as its mentioned in the document !!!
I am using this .htaccess works for non-root folders too
这里
here
这个回复对你来说可能太晚了,但我昨天也遇到了同样的问题。
听起来问题是 apache 没有重写 url。当尝试在 OSX 10.7 上运行 F3 时,我遇到了同样的问题 - '
GET /
' 路线可以工作,但不能将 'GET /foo
' 作为 F3 索引。 php 位于 localhost/F3 的子目录中。我的解决方案是:AllowOverride
All
(我的是 None)(文件下方) 。如果没有步骤 3,apache 将忽略任何重写指令。我通过更改本地 WordPress 安装上的永久链接发现了这一点,但它们失败,表明问题是 apache 配置,而不是 F3。
This response may be too late for you but I had the same problem yesterday.
It sounds like the problem is apache is not rewriting urls. I had the same issue when trying to get F3 running on OSX 10.7 - the '
GET /
' route would work but not the 'GET /foo
' as the F3 index.php was in a subdir for localhost/F3. My solution was to:AllowOverride
All
(mine was None) for your web directory in httpd.conf (further down the file).Without step 3, apache will ignore any rewrite directives. I discovered this by changing the permalinks on a local wordpress install and they failed indicating the problem was the apache config, not F3.
我正在使用 MAMP 堆栈运行它。
我将他们的文件夹从“fatfree-master”重命名为“f3”。
我将该文件夹放在 htdocs 旁边。
他们的 .htaccess 文件(现在位于 MAMP/f3/lib 内)保持不变。
根据他们的示例,我的 .htaccess (在我的网络子文件夹中)是库存标准:
希望这对某人有帮助。
I'm running this with a MAMP stack.
I renamed their folder from "fatfree-master" to "f3".
I put that folder next to htdocs.
Their .htaccess file (now inside MAMP/f3/lib) remains untouched.
My .htaccess (in my web subfolder) is stock standard as per their example:
Hope this helps someone.
我遇到了同样的问题,我通过将 .htaccess 文件移动到 fatfree 项目的根目录来解决它。
在根目录创建一个包含以下代码的文件 .htaccess
I had the same issue and i solved it by moving the .htaccess file to the root directory of the fatfree project.
create a file .htaccess with folowing code at root directory