使用htaccess将所有重定向到index.php [英] Redirect all to index.php using htaccess

查看:342
本文介绍了使用htaccess将所有重定向到index.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm 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

如果我将框架安装在其他文件夹中,则可以说它必须重定向回

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,您可以在index.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'] (已于18年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 = Last(在此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 =附加查询字符串,以防万一您想保留诸如?like=penguins之类的东西并传递给index.php.

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.

这篇关于使用htaccess将所有重定向到index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆