【问题标题】:htaccess file-exist check doesnt seem to work when used with apache Alias's与 apache Aliases 一起使用时,htaccess 文件存在检查似乎不起作用
【发布时间】:2020-06-26 17:11:12
【问题描述】:

我已经使用 .htaccess 文件 15 年了,所以我对它们相当有信心。但是今天我发现“文件存在”检查似乎无法与 Apache 的 Alias 命令一起正常工作 - 我不知道为什么不能(我已经多次阅读文档并且找不到任何可以解释的内容这个特别)。

例如这个简单的 http conf:

Alias /site             /sites/site1.com

例如使用这个简单的 htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /site/

# this never matches: (despite being used in most examples all over the web)
RewriteCond %{DOCUMENT_ROOT}/$1.extension -f
# this doesn't match either: RewriteCond %{REQUEST_FILENAME} -f

RewriteRule (.*)$ FailOnPurpose [NC]
</IfModule>

...从不触发。这里的意图是:

  1. 收到请求时...
  2. ...检查是否存在具有该名称和扩展名“extension”的文件
  3. ...如果是这样:重写

例如如果我要求:

http://example.com/site/a.png

还有一个文件:

/sites/site.com/a.png.extension

然后我希望 RewriteCond 能够工作。

如果我从 apache 中删除所有别名,并使用普通的直接子文件夹,那么它可以工作。

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

    您不能在别名目录的上下文中使用DOCUMENT_ROOT,因为DOCUMENT_ROOT 将指向别名路径之外的路径,并且您的条件将始终失败。

    你可以使用这条规则:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME}.extension -f
    RewriteRule (.*) FailOnPurpose [L,QSA]
    

    我已经在我的本地 Apache 2.4 上使用别名 .htaccess 对其进行了测试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-14
      • 2011-01-15
      • 1970-01-01
      • 2020-08-10
      • 2016-01-10
      • 2013-12-16
      • 2017-07-31
      • 1970-01-01
      相关资源
      最近更新 更多