简体   繁体   English

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

[英]htaccess “if file exists” and “mod_rewrite -f” not working

I have some problems with a Cache Plugin for ProcessWire but maybe this is a more global question about apache .htaccess restrictions. 我的ProcessWire缓存插件存在一些问题,但这也许是有关apache .htaccess限制的更广泛的问题。

Example: 例:

I call my page like this: http://example.com/foo The .htaccess should look up if there is a file DOCUMENT_ROOT/cache/foo/index.html If the file exists, then show the file, or - simplified for this example - just call a success page 我这样称呼我的页面: http : //example.com/foo如果有文件DOCUMENT_ROOT/cache/foo/index.html则应查找.htaccess。如果该文件存在,则显示该文件,或者-简化为此示例-只需调用成功页面

Example File structure: 示例文件结构:

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>

This works properly on my local machine ([SERVER_SOFTWARE] => Apache/2.4.18 (Unix)) But not with the hosting package where I want to use. 这可以在我的本地计算机上正常运行([SERVER_SOFTWARE] => Apache / 2.4.18(Unix)),但不适用于我要使用的主机软件包。 ([SERVER_SOFTWARE] => Apache/2.2.31 (Unix)) The reaction of the server is just a 404: "The requested URL /foo was not found on this server." ([SERVER_SOFTWARE] => Apache / 2.2.31(Unix))服务器的响应只是一个404:“在此服务器上找不到请求的URL / foo”。

Is it not working because of the version of apache or is it restricted by the hoster? 是因为apache的版本而无法使用,还是受到托管者的限制?

Btw. 顺便说一句。 the ModRewrite basically works with this hosting. ModRewrite基本上可以与此托管一起使用。 For example like this: 例如这样:

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

if i call http://example.com/bar , file-found.php is shown 如果我致电http://example.com/bar ,则显示file-found.php

Any ideas how to alternatively solve this "if file exists => then" rule in the htaccess? 任何想法如何解决htaccess中的“如果文件存在=>则”规则?

Thanks for help! 感谢帮助!


EDIT: even more simplified: 编辑:更加简化:

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

works local, but not on hoster 在本地工作,但不在托管人上工作

I think that in RewriteCond the special characters should be escaped with \\ (if that whole phrase is not in double quotes) 我认为在RewriteCond ,特殊字符应使用\\进行转义(如果整个短语不在双引号中)

in RewriteRule did you try to add flags, like [L] or etc.. RewriteRule中,您是否尝试添加标志,例如[L]等。

Try this (you may need RewriteBase / ): 试试这个(您可能需要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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM