特定文件的mod_rewrite异常 [英] mod_rewrite exception for a specific file

查看:75
本文介绍了特定文件的mod_rewrite异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我的.htaccess文件被设置为:

My page is not redirecting as it should due to my .htaccess file which is set as:

RewriteEngine on  
RewriteCond $1 !^(index\.php|resources|robots\.txt)  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^(.*)$ index.php/$1 [L,QSA]   

我将此设置用于我的MVC框架,所以我会得到 / controller / method / argument 这样的网址,但是当我重定向到/forum/login.php时,它会切成/ forum/。

I use this setup for my MVC Framework so i get urls like /controller/method/argument however when I redirect to /forum/login.php it cuts to /forum/.

如何将其添加为例外,以便能够重定向到 /forum/login.php

How can I add this as an exception so that i will be able to redirect to /forum/login.php

我在/ forum /目录中找到了另一个.htaccess,这是否也可能引起问题?

I found another .htaccess in my /forum/ directory could this be causing the problem as well?

# BEGIN PunBB

<IfModule mod_rewrite.c>
    # MultiViews interfers with proper rewriting
    Options -MultiViews

    RewriteEngine On

    # Uncomment and properly set the RewriteBase if the rewrite rules are not working properly
    #RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . rewrite.php [L]
</IfModule>


推荐答案

首先,我将告诉您如何阅读RewriteRule :

First I'll tell you how to read your RewriteRule:

您从第一个(或下一个)RewriteRule条目开始:

You start with the first (or next) RewriteRule entry:

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

第一个参数是可以匹配您所请求的正则表达式网址。 ^(。*)$ 匹配所有内容,并将此所有内容存储在一个变量中,以便以后使用。

The first parameter is a regular expression that can match your requested URL. ^(.*)$ matches everything, and stores this "everything" inside a variable that can be used later.

仅在前面有RewriteCond条目时,才会对它们进行下一个评估:

Only if there are preceding RewriteCond entries, they are evaluated next:

RewriteCond $ 1!^(index\.php | resources | robots txt.txt)

$ 1 是对内部匹配内容的引用RewriteRule的第一个括号。将此与第二个参数进行比较,第二个参数是一个表示几个显式名称的正则表达式,而取反该表达式,例如仅当正则表达式不匹配时,此规则才允许执行RewriteRule。如果此条件返回true,则将查看下一个条件。

$1 is a reference to the content matched inside the first parentheses of RewriteRule. This is compared to the second parameter, which is a regular expression stating several explicit names, and the ! negates the expression, e.g. this rule allows execution of the RewriteRule only if the regex does not match. If this condition returns true, the next condition will be looked at.

RewriteCond%{REQUEST_FILENAME}!-f

如果请求的文件名不是硬盘上的真实文件,则此条件为true。

If the requested filename is no real file on the harddisk, this condition is true.

RewriteCond%{REQUEST_FILENAME}!-d

如果请求的文件名不是真实目录,则此条件为真。

If the requested filename is no real directory, this condition is true.

仅当所有这些条件都为真(它们与AND链接在一起)时,我们才返回重写规则:

Only if all these conditions are true (they are chained together with AND), we come back to the rewrite rule:

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

此结果重写步骤定义为第二个和第三个参数。 $ 1 再次与匹配内容一起使用,并且参数定义此规则(如果最初匹配)将是最后一个规则(L),并且任何查询重写目标中定义的字符串将附加到原始URL(QSA)中的任何查询字符串上。

The result of this rewrite step is defined as the second and third parameter. $1 is used again as with the content of the match, and the parameters define that this rule, if initially matched, will be the last rule (L), and that any query string defined in the rewrite target will be appended to any query string in the original URL (QSA).

评论:

MVC框架的通常重写尝试尽可能地提高性能。您的重写条件都必须经过评估才能成功重写。仅当任何RewriteCond返回false时,才会停止。每个被重写的请求都要经过大量的CPU密集测试。首先是RewriteRule正则表达式,然后是第一个RewriteCond中的正则表达式,然后是文件系统上的两个硬盘测试是否存在文件。

The usual rewriting for MVC frameworks try to be as performant as possible. Your rewrite conditions all have to be evaluated for a successful rewrite. The will stop only if any of the RewriteCond return false. Every request that gets rewritten is subject to plenty of cpu intensive tests. First the RewriteRule regex, then the regex in the first RewriteCond, followed by two harddisk tests on the filesystem for file existance.

另一方面,第一个RewriteCond似乎可以没必要。它测试某些名称,如果找到,则中止重写。第二个RewriteCond应该检测到 index.php,因为它是一个现有文件(如果没有,重写将如何工作)。以 resources开头的所有内容也将匹配,但出于相同的原因,可能不应该匹配:现有资源将在第二个RewriteCond中找到。最后一个 robots.txt文件。如果您想避免在机器人抓取您的网站时出现404错误,最好有一个可能是空的。

On the other hand, the first RewriteCond seems to be unnecessary. It tests for certain names, and if found, aborts the rewriting. "index.php" should be detected by the second RewriteCond, because it is an existing file (how would the rewriting work if not). Anything starting with "resources" will also be matched, but probably shouldn't for the same reasons: Existing resources will be found by the second RewriteCond. Last the "robots.txt" file. It is always a good idea to have one, possibly emtpy if you want to avoid 404 when robots fetch your site.

由于您没有更改查询字符串中的任何内容,

As you are not changing anything in the query string, the [QSA] directive is not needed.

改进:

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

第一个RewriteRule将匹配整个请求的路径。两个RewriteCond与[OR]连接,因此第一个返回true的RewriteCond将取消进一步的评估。第一个RewriteCond测试请求的文件是否存在。如果存在,则返回true,然后处理返回到第一个RewriteRule。目标表达式是-,表示请勿重写。 [L]停止进一步处理重写规则。因此,最后,对于一个现有文件,我们只有一个正则表达式和一个文件系统测试,然后,该现有文件将被发送到浏览器。

The first RewriteRule will match the whole requested path. The two RewriteCond are connected with [OR], so the first RewriteCond that returns true will cancel further evaluation. The first RewriteCond tests if the requested file exists. If it exists, it returns true, and the processing returns to the first RewriteRule. The target expression is "-", which means "do not rewrite". The [L] stops further processing of rewrite rules. So in the end, for an existing file we only have one regex and one filesystem test, and after that, this existing file will be sent to the browser.

如果没有找到文件后,第一个RewriteRule和RewriteCond将不会触发,因此[L]不会停止该过程。因此,第二个RewriteRule被执行。这是无条件的,并且正则表达式与以前相同,匹配所有内容并将其重写为 index.php。

If no file was found, the first RewriteRule and RewriteCond will not trigger, so the [L] there will not stop the process. So the second RewriteRule is executed. This one is unconditional, and the regex is the same as before, matching everything, and rewriting it to "index.php".

此重写不会调用您的索引。 php(如果有),包括/forum/login.php。

This rewriting will not call your index.php if any file exists, including /forum/login.php.

您可以将第二个更改为 RewriteRule ^。* $ index.php / $ 0 [L] 如果您想继续解析 $ _ SERVER ['PATH_INFO'] 而不是 $ _ SERVER [' REQUEST_URI']

You can change the second to RewriteRule ^.*$ index.php/$0 [L] if you do want to continue parsing $_SERVER['PATH_INFO'] instead of $_SERVER['REQUEST_URI'].

这篇关于特定文件的mod_rewrite异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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