重定向所有的index.php htaccess的 [英] Redirect all to index.php htaccess
问题描述
我道歉,关于这一主题的第千万问题,但我只是无法得到它的工作我想它的方式。
My apologies for the 10000000th question on this topic, but i just can't get it to work the way i want it.
我在写一个简单的PHP mvc'ish框架。我希望这个框架能够被安装在任何目录下。
I'm writing a simple php mvc'ish framework. I want this framework to be able to be installed in any directory.
什么我的PHP脚本的作用: 抓住请求URI,打破它分成几部分。使得段1中的控制器和段2的动作。这正好都很好,当我做到这一点:
What my php script does : Grabs the request uri and breaks it off into segments. Making segment 1 the controller and segment 2 the action. This goes all fine when i do this:
http://www.domain.com/mvc/module/test/
这将前往特定模块控制器和方法。 现在我有一个默认的控制器,家居控制器,这是在文件夹中的家。
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.domain.com/mvc/home/ 它会显示一个403禁止的,因为这个文件夹不存在,相反,它也应该回去 HTTP://www.domain。 COM / MVC / index.php文件
Now when i acces this folder directly http://www.domain.com/mvc/home/ It will display a 403 forbidden, because this folder does exist, instead it should also go back to http://www.domain.com/mvc/index.php
如果我将不得不安装在不同的文件夹中的框架,可以说文件夹框架具有重定向回 HTTP:/ /www.domain.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.domain.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]
我真的希望有人能帮助我,或点我到正确的方向!
I really hope someone could help me out or point me into the right direction!
非常感谢!!
推荐答案
您重写规则看起来几乎确定...
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]
目前你只是匹配的。
这是任何字符的一个实例,你需要至少。* $ c中的那一刻$ C>,我用
^([^] *)$
这是任何数量的非?匹配任意数量的任何字符的实例字符。
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, I'm using ^([^?]*)$
which is any number of "non-?" characters.
如果你想整个事情安装在一个子目录,如 / MVC /
或 /框架/
最不复杂的方式来做到这一点是稍微改变重写规则,考虑到这一点。
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.
重写规则^([^] *)$ /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.
在 $ PATH
变量将包含伪造的目录结构,因此 / MVC /模块/测试
例如,然后你就可以使用index.php来确定要执行的控制和操作。
The $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.
的标志:
NC =任何情况下(不区分大小写是没有必要的,因为有模式中的任何字符)
NC = No Case (not case sensitive, not really necessary since there are no characters in the pattern)
L =上次(它会停止在改写后,这个改写所以一定要确保它在你重写的列表中的最后一件事)
L = Last (it'll stop rewriting at after this Rewrite so make sure it's the last thing in your list of rewrites)
QSA =查询字符串Apend,以防万一你有像?像=企鹅
上要保持并传递给index.php文件结束。
QSA = Query String Apend, just in case you've got something like ?like=penguins
on the end which you want to keep and pass to index.php.
这篇关于重定向所有的index.php htaccess的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!