将所有请求转发到一个index.php文件(对于不存在的文件),但*包括* index.php本身 [英] Forward all requests to one index.php file, for non-existing files, but *including* the index.php itself

查看:352
本文介绍了将所有请求转发到一个index.php文件(对于不存在的文件),但*包括* index.php本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在.htaccess文件中有一个非常典型的单入口点配置:

So I have this quite typical "single point of entry" config in the .htaccess file:

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

它很好用。现在-我还将对index.php文件本身的请求进行何种操作。因此,我这样做:

And it works fine. Now - I also what that behavior on the request to the index.php file itself. So I do this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
#file must not exist:
RewriteCond %{REQUEST_FILENAME} !-f [or]

# *unless* this is the index.php *itself*, then I also 
#want to pass it to itself. 
RewriteCond %{REQUEST_FILENAME} index.php

RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

所以我们这里有种例外-在文件不存在时继续,但在文件存在但仍是'index.php'时继续。

So we have kind of exception here - go on when file doesn't exist but also when it exists but it's the 'index.php'.

它可以工作,但不幸的是,它也适用于结构更深的文件。我想要的是仅捕获与.htaccess本身处于同一级别的'index.php'。

And it works, but unfortunately it also works for files deeper in the structure. What I want is to catch only the 'index.php' being on the same level as the .htaccess itself.

更新:
也许我应该澄清一下。我想为PHP创建一个非常通用的开箱即用的单入口点,像这样:

Update: Maybe I should clarify. I want to create a very generic, out of the box single point of entry for php, like this one:

https://github.com/kpion/point1

有效,但我觉得我应该做些不同的事情。我已经问过类似的问题(使单个入口点(前端控制器)与最常见的Web服务器设置一起工作的最佳方法),但被认为是太长了,所以这是我的第二种方法:)

It "works", but I feel I should do it a bit differently. I already asked a similar question ( The utlimate way to make a single entry point (front controller) working with the most common web server setups ) but was considered as "too long" so this is my second approach :)

请记住,在这种情况下,我不能使用^ / index.php,因为'request uri确实不需要以它开头。 htaccess也可以更深,即在文档根目录中更深。当我在/ var / www / html中有许多小型项目并且不想为所有内容创建虚拟主机时,这实际上很常见。因此,根是/ var / www / html,现在是:

Please bear in mind, that I can't use ^/index.php - in the condition, because the 'request uri doesn't really need to start with it. This htaccess can be deeper as well, i.e. deeper in the document root. This is actually quite common, when I have many small projects in /var/www/html and I don't want to create a vhost for everything. So the "root" is /var/www/html and now:

示例:

该项目是位于
/ var / www / html / point / point1

The project is located in /var/www/html/point/point1

.htaccess位于其中。它将所有对不存在文件的请求转发到index.php。除了几行外,它还将自身转发到index.php。

The .htaccess is inside it. It forwards all requests to non-existing files to the index.php. With my few additional lines, it also forwards itself to the index.php.

注意:在这种情况下,%{REQUEST_URI}打开/ var / www / html / point / point1等于 /point/point1/index.php

Note: %{REQUEST_URI} in this case, when we open /var/www/html/point/point1 is equal to "/point/point1/index.php"

不是 / index.php

It is not /index.php

当我调用 http://localhost/point/point1/.local/noExistinFile http://本地主机:82 / point / point1 / (这将指向index.php,它将与我的上一个RewriteCond相匹配)。

It works fine, when I call http://localhost/point/point1/.local/noExistinFile or http://localhost:82/point/point1/ (this will point to index.php, which will be matched by my last RewriteCond).

但是,当我在/var/www/html/point/point1/.local/index.php中有一个文件,我叫 http://localhost2/point/point1/.local/index.php -我希望它通过条件,但我不希望它通过被转发到索引.php。

But, when I have a file located in /var/www/html/point/point1/.local/index.php and I call http://localhost2/point/point1/.local/index.php - I don't want this to pass the conditions and I don't want it to be forwarded to index.php.

所以我需要相对于.htaccess位置相对的东西。

So I need something relative to the .htaccess location.

推荐答案

啊,我忘了问这个问题了。对于任何有兴趣的人:我发现解决方案非常简单:

Ah, I forgot I asked this question. For anyone interested: I found out the solution is quite simple:

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

# lets forward the index.php itself as well, to be later consistent in how we handle things:
RewriteRule ^(index.php)$ index.php?url=index.php [QSA,L]

最后一行可以完成任务,因为:

The last line does the job, because:


  1. 上面的RewriteCond显然不适用(默认情况下,它们仅适用于下面的 RewriteRule)。因此-f(必须不存在)不适用,这很酷。

  1. The RewriteCond above obviously does not apply (by default they only work for the "RewriteRule" directly below). So the -f (must not exist) doesn't apply, which is cool.

现在关键部分:已测试的字符串 > RewriteRule中的 是目录.htaccess的文件/目录相对。因此,如果.htaccess文件位于/ var / www / html / point / point1,然后我们请求:

Now the key part: the tested string in the RewriteRule is a file/dir relative to the directory .htaccess is in. So, if the .htaccess file is inside /var/www/html/point/point1 and we request:

http:// whateverHostWeSetPointingToThisDir / index。 php ,那么在RewriteRule中测试的字符串就是 index.php。而当我们请求

http://whateverHostWeSetPointingToThisDir/index.php then the string being tested in RewriteRule is exactly "index.php". And when we request

http:/ /localhost/point/point1/blah/index.php 然后,假设... point1指向我们的 point1目录,所测试的正是 blah / index.php。

http://localhost/point/point1/blah/index.php then, assuming the ...point1 points to our 'point1' dir, what is tested is exactly "blah/index.php".

这就是为什么RewriteRule ^(index.php)$匹配同一目录中的 index.php文件的原因目录作为.htaccess本身。无论主机名,父目录名或任何其他名称。

This is why RewriteRule ^(index.php)$ matches the 'index.php' file being in the same directory as the .htaccess itself. Regardless the host name, or the parent directory name, or anything.

顺便说一句:如果我们请求 http:// whateverHostWeSetPointingToThisDir ,如果我们设置了DirectoryIndex index.php,这也将起作用,但这并没有真正的关系。

By the way: if we request http://whateverHostWeSetPointingToThisDir then this will work as well if we have set DirectoryIndex index.php but that's not really related.

这篇关于将所有请求转发到一个index.php文件(对于不存在的文件),但*包括* index.php本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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