是否可以通过.htaccess
保护单个URL?例如,我将有website.com/index.php?skin=name
。我可以密码只保护这个网址吗? (没有PHP更改,只有.htaccess
).htaccess代码保护单个网址?
website.com/index.php
或website.com/index.php?skin=other_name
不应受限制。
是否可以通过.htaccess
保护单个URL?例如,我将有website.com/index.php?skin=name
。我可以密码只保护这个网址吗? (没有PHP更改,只有.htaccess
).htaccess代码保护单个网址?
website.com/index.php
或website.com/index.php?skin=other_name
不应受限制。
您可以重写地址来保护它是这样的:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^skin=(name)$
RewriteRule ^(.*)$ /skin/%1 [PT]
<LocationMatch "/skin/name">
AuthType Basic
AuthName "By Invitation Only"
AuthUserFile /path/to/passwords
Require valid-user
</LocationMatch>
如果您使用mod-rewrite将地址重写为其他内容并保护该地址,则可以这样做。
https://serverfault.com/questions/94964/apache-locationmatch-directive
不能使用LocationMatch,即使它会出现,你可以。
你最好打赌就是用PHP来做。快速谷歌搜索后,我发现使用PHP_AUTH一个伟大的方式,看起来很像的.htaccess登录提示
<?php
$login_successful = false;
// check user & pwd:
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])){
$usr = $_SERVER['PHP_AUTH_USER'];
$pwd = $_SERVER['PHP_AUTH_PW'];
if ($usr == 'jonas' && $pwd == 'secret'){
$login_successful = true;
}
}
// login ok?
if (!$login_successful){
// send 401 headers:
// realm="something" will be shown in the login box
header('WWW-Authenticate: Basic realm="Secret page"');
header('HTTP/1.0 401 Unauthorized');
print "Login failed!\n";
}
else {
// show secret page:
print 'you reached the secret page!';
}
您不能在.htaccess文件中使用LocationMatch – 2016-11-08 15:10:03