如何进行有条件的 .htaccess 密码保护
我正在尝试使用 .htaccess 对特定网址进行密码保护。不同的 url 指向相同的文件,但工作方式不同。我现在只需要用密码保护一个网址。我正在尝试使用 setenvif 来执行此操作,但它似乎不起作用。我可能不完全理解 apache setenv 模块的目的或用途。
这是我的代码,似乎不起作用
SetEnvIfNoCase Host "topasswordprotect\.domain\.com$" protecturl
<IfDefine protecturl>
Require valid-user
AuthName "Please enter your name and password"
AuthType Basic
AuthUserFile .htpasswd
</IfDefine>
I'm trying to password protect a specific url using a .htaccess. Different urls point to the same files but have different workings. I now need to password protect only one url. I'm trying to do this using setenvif but it doesn't seem to work. I might not fully understand the purpose or use of the apache setenv module.
This is my code what doesn't seem to work
SetEnvIfNoCase Host "topasswordprotect\.domain\.com$" protecturl
<IfDefine protecturl>
Require valid-user
AuthName "Please enter your name and password"
AuthType Basic
AuthUserFile .htpasswd
</IfDefine>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我终于想通了。我确实不得不使用 mod_rewrite 引擎。我决心找到一个不需要创建额外文件或目录的解决方案。
相同的文件,没有额外的目录,一个 .htaccess:
I finally figured it out. I indeed had to use the mod_rewrite engine. I was determined to have a solution which didn't involve me having to make extra files or directories.
Same files, no extra directories, one .htaccess:
好吧,这实际上可以在没有 mod_rewrite 的情况下通过
SetEnvIf
来实现。我需要它作为开发网站来测试 Facebook OpenGraph 标签的工作原理。
IfDefine
适用于与环境变量不同的定义。引用自 apache 文档:
Well, this can actually be achieved without mod_rewrite and with
SetEnvIf
.I needed it for dev site to test how Facebook OpenGraph tags work.
The
IfDefine
works on defines which are not same as environment variables.A quote from apache docs:
可能无法直接直接实现,但您是否可以有另一个受密码保护的目录,其中链接有原始目录?您可以使用 mod_rewrite 将匹配请求重定向到受密码保护的目录。
Might not be directly possible directly but could you have another password protected directory which has the original directory linked into it? You could the use mod_rewrite to redirect the matching requests to password protected directory.