[英]apache password protect specific URL
我需要对特定URL进行密码保护,例如任何人都可以访问index.php,但是一旦他们添加了参数或“ GET”变量,我就必须要求有效用户。
index.php->甜
index.php?prev = 1->需要有效用户
有趣的问题。
这是一个解决方案:
<Location /fakeindex.php>
Order Deny,Allow
Deny from All
Allow from env=REDIRECT_STATUS
AuthType Basic
AuthUserFile /pathto/htpasswd/file/passwords.secret
AuthName This is an example auth
Require valid-user
</Location>
<Directory /path/to/doc/root>
Order allow,deny
allow from all
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_URI} index.php
RewriteRule .* fakeindex.php [L,QSA]
</Directory>
这部分:
Order Deny,Allow
Deny from All
Allow from env=REDIRECT_STATUS
是否可以防止直接访问/fakeindex.php(但实际上谁想强制直接访问受保护的区域)。 这是可选的。
关键是这些条件规则检测到一个非空的查询字符串,以及对index.php的请求:
RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_URI} index.php
如果它们符合规则:
RewriteRule .* fakeindex.php [L,QSA]
在内部将请求重定向到/fakeindex.php,并使用QSA保留查询字符串。 之后,将应用位置,并且在位置上进行身份验证。
您可以在get变量上使用isset()方法,例如:
if (isset($_GET['prev'])) {
//load password input form
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.