htaccess:仅允许通过完整路径访问文件,而不是重写的路径

htaccess:仅允许通过完整路径访问文件,而不是重写的路径

在我的设置中,我将 CMS 驻留在子文件夹 /sub 中,并将对根目录的所有访问权限重写到此子文件夹:

## Rewrite www.example.com to the CMS home page: www.example.com/corporate/index.php
RewriteRule ^$ sub/index.php [L]

## Let the CMS process paths like www.example.com/corporate/about
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sub/(.*)$ sub/index.php?q=$1 [L,QSA]

## Search for real CMS files that moved, 
##  (e.g. www.example.com/flash/intro.swf to www.example.com/corporate/flash/intro.swf)
RewriteCond %{DOCUMENT_ROOT}/sub/$1 -f
RewriteRule ^(.*)$ sub/$1 [L]

## Let CMS process all paths that are not real files and directories, like www.example.com/about
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ sub/index.php?q=$1 [L,QSA]

因此,现在 CMS 中有一个文件,出于维护原因,我希望能够调用它,但出于安全原因,我不希望从根目录访问此文件。因此,

www.example.com/maintnance

不应该起作用

www.example.com/sub/maintnance

应该管用。

这可以用 .htaccess 来实现吗?

答案1

这是可能的 - 你只需要为该文件添加一条特殊规则您将获得其余的重写内容。例如:

RewriteRule /maintnance$ - [F,L]

表示如果文件名是“/maintnance”,则不要重写 - 而是给出 403 Forbidden 响应并停止重写。

但这不太安全。如果我是你,我会使用某种形式的访问控制,要么基于 IP 地址,要么基于身份验证。

相关内容