Fat Free 框架中 mod_rewrite 的简单问题

发布于 2024-10-09 23:34:41 字数 1208 浏览 6 评论 0原文

我正在尝试设置和学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

忱杏 2024-10-16 23:34:41

所以我的朋友实际上帮我解决了这个问题。我遇到了完全相同的问题,但基本上我也使用 MAMP,并将所有无脂肪文件放在 MAMP htdocs 内的 fatfree 目录中。

解决方案是您需要将 RewriteBase 修改为指向 /[dirname]/ 而不仅仅是 /,然后更改 RewriteRule< /code> 到 /[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,请编辑 /Applications/MAMP/conf/apache 中的 httpd.conf 文件,并确保更改以下内容:

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

基本上

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

更改 <代码>无到全部

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 change RewriteRule to /[dirname]/index.php.

My .htaccess looks like this:

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

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:

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

to

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

Basically change None to All.

樱花落人离去 2024-10-16 23:34:41

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

If you're running F3 under a subfolder, you must change the RewriteBase in .htaccess to match the folder.

情感失落者 2024-10-16 23:34:41

在您的 .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');

as

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

和页面应该显示...

这样做的原因是,如果您只是转到 / 目录(或 /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 modules/mod_rewrite.so

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

或者..我想你可以说:

LoadModule rewrite_module modules/mod_rewrite.so

在 htaccess 文件的开头...

In your .htaccess you have 'index.php' it needs a slash ... '/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]    

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

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

You could also try a global route for the directory

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

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

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

as

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

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:

#LoadModule rewrite_module modules/mod_rewrite.so

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:

LoadModule rewrite_module modules/mod_rewrite.so

at the start of your htaccess file...

说不完的你爱 2024-10-16 23:34:41

我为此苦恼了两天。我调试了 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]

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

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]
奈何桥上唱咆哮 2024-10-16 23:34:41

注意:如果您想以其他名称调用 .htaccess 文件,可以使用 AccessFileName 指令更改文件名。例如,如果您更愿意调用文件 .config,那么您可以将以下内容放入服务器配置文件中:

AccessFileName .config  

这里

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

椵侞 2024-10-16 23:34:41

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

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

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

如果没有步骤 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:

  1. ensure the .htaccess was set as you have.
  2. ensure mod_rewrite.so was enabled in apache's httpd.conf
  3. ensure that you set AllowOverride All (mine was None) for your web directory in httpd.conf (further down the file).
  4. restart apache

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.

静水深流 2024-10-16 23:34:41

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

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

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

希望这对某人有帮助。

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:

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

Hope this helps someone.

雄赳赳气昂昂 2024-10-16 23:34:41

我遇到了同样的问题,我通过将 .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]

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

# 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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文