htaccess 将所有请求重写到单个控制器

发布于 2024-09-25 05:34:20 字数 599 浏览 0 评论 0原文

与 codeigniter 类似,我想捕获所有请求并将它们重写到位于我的 Web 可访问文件夹的根目录中的 index.php 文件。但与 codeigniter 上的示例不同,我不想在重写之前检查文件是否存在(因为我不想允许直接访问任何文件)。

我使用以下重写规则取得了部分成功:

RewriteRule ^(.*)$ index.php/$1/ [L]

但是,我可以让它工作的唯一方法是在它之前添加一个重写条件,它会进行某种检查以确保请求不是针对index.php,否则我得到500 内部服务器错误。我查看了错误日志,似乎是由于递归过多造成的。我明白,如果请求index.php,无论如何都没关系,因为它们会命中正确的文件,但我不明白这种情况的必要性,当我不知道为什么它必须在那里时,我觉得包括它很脏?

工作规则是......

RewriteCond %{REQUEST_URI} !^/index.php/

还值得一提的是,这个重写条件也有效,但我再次不知道为什么需要它!

RewriteCond %{REQUEST_FILENAME} !index.php

In a similar way to codeigniter I want to catch all requests and rewrite them to an index.php file which sites in the root of my web accessible folder. Unlike the examples on codeigniter however I do not want to check whether the file exists before I rewrite it (because I dont want to allow direct access to any files).

I have had partial success using the following rewrite rule:

RewriteRule ^(.*)$ index.php/$1/ [L]

However the only way I can get it to work is by adding a rewrite condition before it which does some sort of check to make sure the request is not for index.php, otherwise I get a 500 internal server error. Ive had a look at the error log and it seems it is due to too much reccursion. I understand that if the request index.php it doesnt matter anyway because they will hit the right file but I dont understand the need for this condition and I feel dirty including it when I dont know why it has to be there?

The working rule is...

RewriteCond %{REQUEST_URI} !^/index.php/

Its also worth mentioning that this rewrite condition worked aswell but again I dont know why its needed!

RewriteCond %{REQUEST_FILENAME} !index.php

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

霓裳挽歌倾城醉 2024-10-02 05:34:20

RewriteCond 意味着,仅当满足这些条件时才会评估以下 RewriteRule。
在您的情况下,换句话说“如果请求的 URI 以 /index.php 开头,则重新路由到 index.php”。

您需要进行该检查,因为否则您将在无限循环中将 index.php 重新路由到自身。

RewriteCond means, the following RewriteRule is only evaluated if these conditions are met.
In your case, in words "If the requested URI doesn't start with /index.php, then reroute to index.php".

You need that check, because otherwise you would be rerouting index.php to itself in an infinite loop.

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