【问题标题】:Exclude a file in a directory from apache .htaccess password protected site从 apache .htaccess 密码保护站点中排除目录中的文件
【发布时间】:2020-07-15 10:47:59
【问题描述】:

我有密码保护我的应用程序目录,但出于某种原因,我必须公开访问文件夹中的文件。我研究并尝试了很多方法,但它不起作用。以下是我的代码。

# Welcome to your htaccess file.
# Remember that modifying this file can break the entire website
# so please edit carefully.

RewriteEngine On
RewriteBase /cms
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /cms/index.php/$1 [L]

<IfModule mod_env.c>
    SetEnv CI_ENV production
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

AuthType Basic
AuthName "Restricted Area"

AuthUserFile /home/root/.htpasswd
require valid-user

# allow public access to the following resources
#SetEnvIf Request_URI "(folder/)$" allow
#SetEnvIf Request_URI "(path/folder\file.php)$" allow-Uri

# Deny by default 
# Order deny,allow
# Deny from all

Order allow,deny
#Allow from env=allow-Uri

<Files "path\to\file.php">
  Allow from all
</Files>

Satisfy ANY

我的项目文件夹结构如下:

root-folder
--sub-folder
  --file.php

截图: https://ibb.co/R9j0QTW

【问题讨论】:

  • Allow/Order/Deny/Satisfy 已弃用。如果您的 Apache >= 2.4,Require env allow-Uri 应该可以工作(&lt;RequireAny&gt; 块在这里是隐含的)。 (固定模式后)

标签: apache .htaccess


【解决方案1】:

这是应该怎么做的:

SetEnvIfNoCase Request_URI "^/path/to/file\.php" ALLOW_URI

AuthType Basic
AuthName "Restricted Area"
AuthUserFile /home/root/.htpasswd
Require valid-user
Satisfy    any
Order      deny,allow
Deny from  all
Allow from env=ALLOW_URI

【讨论】:

  • 带有用户名和密码的弹出窗口在整个应用程序中以相同的方式出现。
  • domain.com/root-folder/sub-folder/file.php?abc=123
  • 您所附的屏幕截图显示了一个不同的 URL,但您在 .htaccess 中有不同的 ALLOW_URI
  • 抱歉截图有误。这是一个正确的ibb.co/R9j0QTW@anubhav
猜你喜欢
  • 2011-07-12
  • 1970-01-01
  • 2010-11-28
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 2017-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多