htaccess“如果文件存在”和“mod_rewrite -f”不起作用

时间:2016-10-12 05:26:09

标签: apache .htaccess mod-rewrite processwire

我在使用ProcessWire的缓存插件时遇到了一些问题,但这可能是一个关于apache .htaccess限制的更全面的问题。

示例:

我这样打电话给我的页面:http://example.com/foo 如果有文件DOCUMENT_ROOT/cache/foo/index.html,则应查找.htaccess 如果该文件存在,则显示该文件,或者 - 为此示例简化 - 只需调用成功页面

示例文件结构:

cache/foo/index.html (some dummy content)
bar (some dummy content)
file-found.php (the success page with some print_r($_SERVER) stuff)
.htaccess with this content:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{DOCUMENT_ROOT}/cache/$1/index.html -f
    RewriteRule ^(.*) file-found.php
</IfModule>

这在我的本地计算机上正常工作([SERVER_SOFTWARE] =&gt; Apache / 2.4.18(Unix)) 但不是我想要使用的托管包。 ([SERVER_SOFTWARE] =&gt; Apache / 2.2.31(Unix))服务器的反应只是404:“在此服务器上找不到请求的URL / foo。”

由于apache的版本是否无法正常工作或是否受到托管服务商的限制?

顺便说一下。 ModRewrite基本上适用于此托管。例如:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(.*) file-found.php
</IfModule>

如果我拨打http://example.com/bar,则会显示file-found.php

如何在htaccess中解决这个“if file exists =&gt; then”规则的任何想法?

感谢您的帮助!

编辑:更简化:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{DOCUMENT_ROOT}/bar -f
  RewriteRule ^(.*) file-found.php
</IfModule>

适用于本地,但不适用于主机

2 个答案:

答案 0 :(得分:0)

我认为在RewriteCond中,特殊字符应使用\进行转义(如果整个短语不是双引号)

RewriteRule中的

是否尝试添加标记,例如[L]等。

答案 1 :(得分:0)

试试这个(您可能需要RewriteBase /):

RewriteCond cache%{REQUEST_URI}/index.html -f
RewriteRule ^[^/]+(?:/[^/]+)*$ cache/$0/index.html [L]
RewriteCond cache%{REQUEST_URI}index.html -f
RewriteRule ^(?:[^/]+/)*$ cache/$0index.html [L]