apache - 只允许访问目录索引

标签 apache .htaccess basic-authentication

我整个上午都在摆弄这个,我真的希望有人能帮助我。我的网站上有一个目录,只有管理员才能访问。所以我使用 .htaccess 文件密码保护它。但我希望 index.php 保持可访问。

所以我试过:

AuthUserFile "/home/example.com/passwords/.htpasswd"
AuthType Basic
AuthName "Admin"
require valid-user

<FilesMatch "^index\.php$">
    Satisfy Any
    Allow from all
</FilesMatch>

是的,它有效,但前提是您在 URL 中明确指定 index.php:

http://www.example.com/admin/index.php -> 好的

http://www.example.com/admin/ -> 关于登录的弹出窗口

http://www.example.com/admin -> 关于登录的弹出窗口

所以我继续修改正则表达式:
<FilesMatch "^(|index\.php)$">
    Satisfy Any
    Allow from all
</FilesMatch>

不是我写过的最好的正则表达式,但嘿,有一些结果:

http://www.example.com/admin/index.php -> 好的

http://www.example.com/admin/ -> 好的

http://www.example.com/admin -> 关于登录的弹出窗口

我只是不明白为什么这不起作用。我也尝试像这样扭转整个事情:
AuthUserFile "/home/example.com/passwords/.htpasswd"
AuthType Basic
AuthName "Admin"

<FilesMatch "^(?!index\.php).+$">
    require valid-user
</FilesMatch>

但再次无济于事......有人看到解决方案吗?

最佳答案

我不知道如何允许访问 http://www.example.com/admin
但是您可以通过重定向对 index.php 的访问来避免该目录被调用
正如你在这里看到的:

RewriteRule ^admin$ admin/index.php

或者如果你需要获取参数
RewriteRule ^admin\/?\?([^#]*)$ admin/index.php?$1

关于apache - 只允许访问目录索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6815874/

相关文章:

c# - 默认发送 BASIC 身份验证,而不是等待 HTTP 401

java - 更改 Apache Storm 拓扑的 Log4J 配置

php - 404 Not found - 为什么我不能在 laravel 中打开除 "/"之外的任何 View ?

python - Apache/WSGI 进程死亡

apache - 如何使用 .htaccess 将 WSS 代理到 WS

php - 自定义网址 - 给每个用户一个网址

node.js - 使用 express 提供静态文件时无法使用基本身份验证

php - 运行 PHP cronjob 超过 15 分钟

php - 在查询参数后从 URL 中删除 index.php 而不使用 .htaccess

java - 使用 Architecture Components MVVM 进行身份验证,将 token 从 Repository 传递到 ViewModel