Php htaccess添加[NC,F]以获取参数

Php htaccess添加[NC,F]以获取参数,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,我正在从事一个MVC项目,我有以下.htaccess文件: Options -Indexes Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?path=$1 [L] RewriteRule !^(public/|index\.php) [NC,F] 它工作正常。我只希

我正在从事一个MVC项目,我有以下
.htaccess
文件:

Options -Indexes
Options -MultiViews

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?path=$1 [L]
RewriteRule !^(public/|index\.php) [NC,F]
它工作正常。我只希望公众可以访问
public/
文件夹和
index.php
文件。所有其他路径都应插入到
path
GET参数中。例如,
mysite.com/controller/method
应该指向
mysite.com/index.php?path=controller/method

现在,有一个问题。当直接访问URL(不包括
index.php
)时,它将
[NC,F]
添加到GET
path
参数中。就像访问
mysite.com
指向
mysite.com/index.php?path=[NC,F]

为什么会发生这种情况,我该如何解决

编辑

我将
index.php
移动到
public/
文件夹中。这是我的
.htaccess
文件:

Options -Indexes
Options -MultiViews

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
RewriteRule ^(/)?$ public/index.php [L]
RewriteRule !^(public/) [NC,F]
Options -Indexes
Options -MultiViews

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
RewriteRule ^(/)?$ public/index.php [L]
RewriteRule !^(public/) [NC,F]
这似乎还可以。我在这方面还有什么改进吗?

添加
重写规则^(/)?$public/index.php[L]
似乎已经解决了这个问题。我不确定这是否是最好的方法,但这是我的
.htaccess
文件:

Options -Indexes
Options -MultiViews

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
RewriteRule ^(/)?$ public/index.php [L]
RewriteRule !^(public/) [NC,F]
Options -Indexes
Options -MultiViews

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
RewriteRule ^(/)?$ public/index.php [L]
RewriteRule !^(public/) [NC,F]

我将
index.php
移到了公用文件夹中,以使事情更清楚。

上一条规则中没有重定向位置,因此它将标记作为重定向位置。只需一个破折号就可以了,因为这是禁止响应。将最后一行更改为:

RewriteRule !^(public/|index\.php$) - [NC,F]
为了清楚起见,还在index.php后面添加了美元符号

编辑:

我建议将您的新规则集更新为以下内容(实际上,我建议您在下面进行彻底的重新思考,但这是对您现有规则的更新):

主页规则中不需要
(/)?
,因为
中不包含开头正斜杠。htaccess
无论如何都匹配

我将主页的规则移到了顶部,否则它将永远不会被使用,因为它与前面的规则匹配(因此路径参数为空时不在那里,这可能是您想要的)

我阻止了/public/中的任何内容被传递到index.php脚本,因为按照您的方式,任何不存在的公共内容都会被传递到您的索引脚本,这似乎不是您想要的

我添加了
RewriteCond%{REQUEST_URI}!=/public/index.php
,因此如果规则处理多次运行,则无法对其自身执行该规则,并创建一个循环,但随后将其取出,因为/public/上的上述匹配无论如何都涵盖了这一点

重新思考

综上所述,我认为检查文件是否不存在,然后只向存在的文件发送禁止响应,而将其他所有内容发送到您的索引脚本是没有意义的。为什么不将所有内容发送到您的索引脚本?这似乎是您真正想要的。我建议您简化为:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
删除主页规则,因为无需担心将空路径参数传递到索引脚本。将逻辑更改为“保留/public/中的任何内容。对于其他内容,请将其传递到index.php脚本。”因此,不需要文件测试,因为脚本可以处理所有内容,也不需要禁止响应,因为没有任何内容可以匹配,所有内容都包含在规则中。您始终可以将禁止返回到脚本中不希望处理的任何内容,而在以前的设置中,对于现有文件URL,您仍然需要执行此操作

最后一次重新思考

最后,如果我可以建议的话,将index.php文件放在网站的根目录中会更干净,这样以后如果您愿意,您可以使用/public/自己的索引文件,所以最后我会将其移回根目录,并将规则更改为:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*)$ index.php?path=$1 [L]

如果你喜欢这一切,那么如果你投票支持我已经接受了答案,我将不胜感激。

你仍然有我在答案中指出的问题。这是一个简单的解决方案,但还有一些其他潜在问题也需要解决。现在很难回答,因为您的答案已更改了问题的代码。真的,你的问题需要更新。我实现了你提到的破折号。你还看到了什么问题?请记住,我将
index.php
文件移动到了
public/
文件夹中。L并不意味着它不会再次运行,它只是停止了一轮规则,所以您需要检查URL在第一次重写规则时是否还没有index.php。htaccess重写集可以运行多次。另外,请接受我的回答,因为它回答了您问题中的问题。那么您将如何构造
.htaccess
文件?现在似乎工作正常,但我对
htaccess
的了解非常有限,我知道我可能错过了很多东西。谢谢,非常感谢:)