2010-04-19 50 views
1

是否可以通过.htaccess保护单个URL?例如,我将有website.com/index.php?skin=name。我可以密码只保护这个网址吗? (没有PHP更改,只有.htaccess.htaccess代码保护单个网址?

website.com/index.phpwebsite.com/index.php?skin=other_name不应受限制。

回答

4

您可以重写地址来保护它是这样的:

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> 
+0

您不能在.htaccess文件中使用LocationMatch – 2016-11-08 15:10:03

0

你最好打赌就是用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!'; 
} 

来源:http://snippets.dzone.com/posts/show/2006