使用<if>指令时,忽略Apache2.4 + htaccess重写

时间:2017-06-15 04:19:09

标签: apache .htaccess mod-rewrite

我得到一个非常奇怪的错误,我希望有人能告诉我,如果我在这里做错了什么。

正在运行的代码

我有一小段.htaccess代码,效果很好。它检查HTML文件是否存在,如果不是,则尝试加载PHP文件。这是代码..

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)\.html$ $1.php [L]
</IfModule>

有问题的代码

当我引入If directive时,此代码会被忽略,无论它是在...之前还是之后。

# We go to http://site.local/index.html in our browser
<IfModule mod_rewrite.c>
    RewriteEngine On

    # Commenting out the if and keeping the rewrite rule inside works
    <If "(req('Host') == 'site.local')">
        # Commenting out everything inside and keeping the if statement works too
        # This rule below is to demonstrate that even though this rule won't
        # match anything it has an affect on the rule outside the if
        RewriteCond %{HTTP_HOST} ^RANDOMRULENOTMATCHED\.
        RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    </If>

    # this rule gets ignored by the above, you can also put these lines above the if, they'll still be ignored
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)\.html$ $1.php [L]
</IfModule> 

所以我的问题是,为什么在.htaccess文件中使用If指令时我的重写条件会被忽略?

作为旁注我知道我可以通过使用更多RewriteCond来解决这个问题,但是我的目标是使用If指令功能,所以请的答案集中在代码中保留If指令。

0 个答案:

没有答案