使用 htaccess 将所有重定向到 index.php

2022-01-30 00:00:00 php .htaccess mod-rewrite

我正在编写一个简单的基于 PHP 的 MVC 框架.我希望这个框架能够安装在任何目录中.

I am writing a simple PHP-based MVC-ish framework. I want this framework to be able to be installed in any directory.

我的 PHP 脚本抓取请求 uri 并将其分成多个段.它使第 1 段成为控制器,第 2 段成为动作.当我这样做时一切正常:

My PHP script grabs the request uri and breaks it off into segments. It makes segment 1 the controller and segment 2 the action. This goes all fine when I do this:

http://www.example.com/mvc/module/test/

它将转到特定的模块控制器和方法.现在我有一个默认控制器,即 home 控制器,它位于 home 文件夹中.

It will go to the specific module controller and method. Now I have a default controller, the home controller, which is in folder home.

现在当我直接访问这个文件夹时 http://www.example.com/mvc/home/它将显示一个 403 禁止 ,因为这个文件夹确实存在,相反它也应该返回到 http://www.example.com/mvc/index.php

Now when I access this folder directly http://www.example.com/mvc/home/ It will display a 403 forbidden , because this folder does exist, instead it should also go back to http://www.example.com/mvc/index.php

如果我将框架安装在不同的文件夹中,假设文件夹框架必须重定向回 http://www.example.com/framework/index.php

If I would have installed the framework in a different folder, lets say folder framework it has to redirect back to http://www.example.com/framework/index.php

我想将每个文件夹和 php 文件重定向回 index.php,其他一切都保持原样.

I would like to redirect every folder and php file back to the index.php, leaving everything else the way it is.

我遇到的第一个问题是它永远不会重定向到正确的文件夹,总是重定向到域根文件夹.

My first problem I encountered was it never redirects to the right folder, always to the domain root folder.

这是我尝试过的:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

推荐答案

你的重写规则看起来差不多了.

Your rewrite rule looks almost ok.

首先确保您的 .htaccess 文件在您的文档根目录中(与 index.php 相同的位置),否则它只会影响它所在的​​子文件夹在(以及其中的任何子文件夹 - 递归).

First make sure that your .htaccess file is in your document root (the same place as index.php) or it'll only affect the sub-folder it's in (and any sub-folders within that - recursively).

接下来对您的规则稍作更改,使其看起来像:

Next make a slight change to your rule so it looks something like:

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

目前您只是在 . 上进行匹配,它是任何字符的 一个 实例,您至少需要 .* 来匹配任意字符的任意数量的实例.

At the moment you're just matching on . which is one instance of any character, you need at least .* to match any number of instances of any character.

$_GET['path'] 变量将包含假目录结构,例如 /mvc/module/test,然后您可以在索引中使用它.php 来确定 Controller 和您要执行的操作.

The $_GET['path'] variable will contain the fake directory structure, so /mvc/module/test for instance, which you can then use in index.php to determine the Controller and actions you want to perform.


如果你想将整个 shebang 安装在一个子目录中,例如 /mvc//framework/ 最简单的方法是稍微改变重写规则考虑到这一点.


If you want the whole shebang installed in a sub-directory, such as /mvc/ or /framework/ the least complicated way to do it is to change the rewrite rule slightly to take that into account.

RewriteRule ^(.*)$ /mvc/index.php?path=$1 [NC,L,QSA]

并确保您的 index.php 位于该文件夹中,而 .htaccess 文件位于文档根目录中.

And ensure that your index.php is in that folder whilst the .htaccess file is in the document root.


$_GET['path'] 的替代品(2018 年 2 月和 19 年 1 月更新)


Alternative to $_GET['path'] (updated Feb '18 and Jan '19)

实际上没有必要(甚至现在也不常见)将 path 设置为 $_GET 变量,许多框架将依赖 $_SERVER['REQUEST_URI'] 来检索相同的信息——通常是为了确定使用哪个Controller——但原理是完全一样的.

It's not actually necessary (nor even common now) to set the path as a $_GET variable, many frameworks will rely on $_SERVER['REQUEST_URI'] to retrieve the same information - normally to determine which Controller to use - but the principle is exactly the same.

这确实稍微简化了 RewriteRule 因为您不需要创建 path 参数(这意味着 OP 的原始 RewriteRule 将现在工作):

This does simplify the RewriteRule slightly as you don't need to create the path parameter (which means the OP's original RewriteRule will now work):

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L,QSA]

但是,在子目录中安装的规则仍然适用,例如

However, the rule about installing in a sub-directory still applies, e.g.

RewriteRule ^.*$ /mvc/index.php [L,QSA]


旗帜:

NC = 不区分大小写(不区分大小写,因为模式中没有字符,所以没有必要)

NC = No Case (not case sensitive, not really necessary since there are no characters in the pattern)

L = 最后(在此 Rewrite 之后它将停止重写,因此请确保它是您的重写列表中的最后一件事)

L = Last (it'll stop rewriting at after this Rewrite so make sure it's the last thing in your list of rewrites)

QSA = 查询字符串追加,以防万一您想保留并传递给 index.php 的末尾有类似 ?like=penguins 的内容.

QSA = Query String Append, just in case you've got something like ?like=penguins on the end which you want to keep and pass to index.php.

相关文章